-
-
Notifications
You must be signed in to change notification settings - Fork 362
[daehyun99] WEEK 07 Solutions #2801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| l = 0 | ||
| seen = set() | ||
| longest = 0 | ||
|
|
||
| for c in s: | ||
| if c not in seen: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 로직이 하단의 while과 겹치지 않을까요? |
||
| seen.add(c) | ||
| longest = max(longest, len(seen)) | ||
| else: | ||
| while c in seen: | ||
| seen.remove(s[l]) | ||
| l += 1 | ||
| seen.add(c) | ||
| return longest | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석number-of-islands/daehyun99.py# Time: O(M * N)
# Space: O(M * N)
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
h = len(grid)
w = len(grid[0])
count = 0
for i in range(h):
for j in range(w):
if grid[i][j] == "1":
stack = []
stack.append([i, j])
while len(stack) > 0 :
x, y = stack.pop()
grid[x][y] = "0"
if x > 0 and grid[x-1][y] == "1":
stack.append([x-1, y])
if x + 1 < h and grid[x+1][y] == "1":
stack.append([x+1, y])
if y > 0 and grid[x][y-1] == "1":
stack.append([x, y-1])
if y + 1 < w and grid[x][y+1] == "1":
stack.append([x, y+1])
count += 1
return count
📊 시간/공간 복잡도 분석
피드백: grid를 순회하며 1인 칸마다 DFS/BFS로 연결된 영역을 탐색한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Time: O(M * N) | ||
| # Space: O(M * N) | ||
| class Solution: | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| h = len(grid) | ||
| w = len(grid[0]) | ||
| count = 0 | ||
| for i in range(h): | ||
| for j in range(w): | ||
| if grid[i][j] == "1": | ||
| stack = [] | ||
| stack.append([i, j]) | ||
| while len(stack) > 0 : | ||
| x, y = stack.pop() | ||
|
|
||
| grid[x][y] = "0" | ||
| if x > 0 and grid[x-1][y] == "1": | ||
| stack.append([x-1, y]) | ||
| if x + 1 < h and grid[x+1][y] == "1": | ||
| stack.append([x+1, y]) | ||
| if y > 0 and grid[x][y-1] == "1": | ||
| stack.append([x, y-1]) | ||
| if y + 1 < w and grid[x][y+1] == "1": | ||
| stack.append([x, y+1]) | ||
| count += 1 | ||
| return count |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석reverse-linked-list/daehyun99.py# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Time: O(N)
# Space: O(1)
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return head
pointer = head
left = None
while pointer.next is not None:
right = pointer.next
pointer.next = left
left = pointer
pointer = right
pointer.next = left
return pointer
📊 시간/공간 복잡도 분석
피드백: 커서 포인터를 이용해 노드를 반전시키며 순차적으로 앞 노드를 가리키게 한다. 개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 문제는 조금 더 코드 라인 수를 확 줄일수 있는 문제에요! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| # Time: O(N) | ||
| # Space: O(1) | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return head | ||
| pointer = head | ||
| left = None | ||
| while pointer.next is not None: | ||
| right = pointer.next | ||
| pointer.next = left | ||
| left = pointer | ||
| pointer = right | ||
| pointer.next = left | ||
| return pointer |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석set-matrix-zeroes/daehyun99.pyclass Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
row = set()
col = set()
for i in range(len(matrix)):
if 0 in matrix[i]:
row.add(i)
for j in range(len(matrix[0])):
if matrix[i][j] == 0:
col.add(j)
for i in row:
matrix[i] = [0] * len(matrix[0])
for i in range(len(matrix)):
if i in row:
continue
for j in range(len(matrix[0])):
if j in col:
matrix[i][j] = 0
📊 시간/공간 복잡도 분석
피드백: 먼저 0이 존재하는 행/열을 수집하고, 두 번째 순회에서 0으로 설정한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class Solution: | ||
| def setZeroes(self, matrix: List[List[int]]) -> None: | ||
| row = set() | ||
| col = set() | ||
|
|
||
| for i in range(len(matrix)): | ||
| if 0 in matrix[i]: | ||
| row.add(i) | ||
| for j in range(len(matrix[0])): | ||
| if matrix[i][j] == 0: | ||
| col.add(j) | ||
|
|
||
| for i in row: | ||
| matrix[i] = [0] * len(matrix[0]) | ||
|
|
||
| for i in range(len(matrix)): | ||
| if i in row: | ||
| continue | ||
| for j in range(len(matrix[0])): | ||
| if j in col: | ||
| matrix[i][j] = 0 | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석unique-paths/daehyun99.pyclass Solution:
def uniquePaths(self, m: int, n: int) -> int:
matrix = [[0] * n for _ in range(m)]
matrix[0][0] += 1
for x in range(m):
for y in range(n):
if x == 0 and y == 0:
continue
elif x == 0:
matrix[x][y] += matrix[x][y-1]
elif y == 0:
matrix[x][y] += matrix[x-1][y]
else:
matrix[x][y] += (matrix[x][y-1] + matrix[x-1][y])
return matrix[m-1][n-1]
📊 시간/공간 복잡도 분석
피드백: 2차원 DP 배열을 사용해 위/왼쪽의 경로 수를 합산한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| matrix = [[0] * n for _ in range(m)] | ||
| matrix[0][0] += 1 | ||
|
|
||
| for x in range(m): | ||
| for y in range(n): | ||
| if x == 0 and y == 0: | ||
| continue | ||
| elif x == 0: | ||
| matrix[x][y] += matrix[x][y-1] | ||
| elif y == 0: | ||
| matrix[x][y] += matrix[x-1][y] | ||
| else: | ||
| matrix[x][y] += (matrix[x][y-1] + matrix[x-1][y]) | ||
| return matrix[m-1][n-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
longest-substring-without-repeating-characters/daehyun99.py
📊 시간/공간 복잡도 분석
피드백: seen 집합과 포인터 l을 사용해 중복 문자가 나오면 왼쪽을 제거하며 부분 문자열 길이를 갱신한다.
개선 제안: 현재 구현이 적절해 보입니다.