-
-
Notifications
You must be signed in to change notification settings - Fork 362
[dolphinflow86] WEEK 07 Solutions #2799
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,20 @@ | ||
| # N is the length of s. | ||
| # TC: O(N) - each character is added and removed at most once | ||
| # SC: O(N) - stores the characters in the current window | ||
| class Solution: | ||
|
|
||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| chars = set() | ||
| left = 0 | ||
| longest = 0 | ||
|
|
||
| for right, char in enumerate(s): | ||
| while char in chars: | ||
| chars.remove(s[left]) | ||
| left += 1 | ||
|
Comment on lines
+12
to
+14
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. left를 하나씩 증가하지 않고 각 문자의 인덱스를 저장해서 점프하는 방식으로도 조금 더 최적화가 가능하니 풀어보셔도 좋을 거 같습니다. |
||
|
|
||
| chars.add(char) | ||
| longest = max(longest, right - left + 1) | ||
|
|
||
| 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/dolphinflow86.py# R is the number of rows, and C is the number of columns.
# TC: O(R * C) - visits each cell at most once
# SC: O(R * C) - uses the recursion stack in the worst case
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
rows = len(grid)
cols = len(grid[0])
def dfs(row, col):
if (
row < 0
or row >= rows
or col < 0
or col >= cols
or grid[row][col] != "1"
):
return
grid[row][col] = "0"
dfs(row - 1, col)
dfs(row + 1, col)
dfs(row, col - 1)
dfs(row, col + 1)
islands = 0
for row in range(rows):
for col in range(cols):
if grid[row][col] == "1":
islands += 1
dfs(row, col)
return islands
📊 시간/공간 복잡도 분석
피드백: 그리드의 각 셀을 한 번씩 방문하고 인접한 '1'들을 재귀적으로 처리한다. 개선 제안: 재귀 깊이가 커질 수 있는 환경에서는 스택 기반 DFS나 BFS로 스택 사용을 명시하는 것이 안전하다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # R is the number of rows, and C is the number of columns. | ||
| # TC: O(R * C) - visits each cell at most once | ||
| # SC: O(R * C) - uses the recursion stack in the worst case | ||
| class Solution: | ||
|
|
||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| rows = len(grid) | ||
| cols = len(grid[0]) | ||
|
Comment on lines
+7
to
+8
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. rows, cols가 길이로 보이지 않고 배열 같은 변수가 담기는 거처럼 보이는 거 같습니다. 길이를 나타내는 네이밍을 사용하시면 어떨까요? |
||
|
|
||
| def dfs(row, col): | ||
| if ( | ||
| row < 0 | ||
| or row >= rows | ||
| or col < 0 | ||
| or col >= cols | ||
| or grid[row][col] != "1" | ||
| ): | ||
|
Comment on lines
+11
to
+17
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. python의 |
||
| return | ||
|
|
||
| grid[row][col] = "0" | ||
|
|
||
| dfs(row - 1, col) | ||
| dfs(row + 1, col) | ||
| dfs(row, col - 1) | ||
| dfs(row, col + 1) | ||
|
|
||
| islands = 0 | ||
|
|
||
| for row in range(rows): | ||
| for col in range(cols): | ||
| if grid[row][col] == "1": | ||
| islands += 1 | ||
| dfs(row, col) | ||
|
|
||
| return islands | ||
|
|
||
|
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/dolphinflow86.py# TC: O(N) - visits each node exactly once
# SC: O(1) - reverses the links in place
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
current = head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prev
📊 시간/공간 복잡도 분석
피드백: 연결 구조를 역전시키면서 상호 참조를 유지하는 표준 패턴이다. 개선 제안: 현재 구현이 적절해 보입니다.
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. 제 개인적인 생각에는요! head를 리턴 혹은 어디에도 쓰지 않으니 current를 쓰시는 부분 그대로 head를 쓰셔도 되지 않을까? 그리고 파이썬의
Contributor
Author
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,16 @@ | ||
| # TC: O(N) - visits each node exactly once | ||
| # SC: O(1) - reverses the links in place | ||
| class Solution: | ||
|
|
||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| prev = None | ||
| current = head | ||
|
|
||
| while current: | ||
| next_node = current.next | ||
| current.next = prev | ||
| prev = current | ||
| current = next_node | ||
|
Comment on lines
+7
to
+13
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. next가 예약어여서 _node를 붙여주신 거 같네요. 다른 네이밍들이랑 일관성을 맞추시는 건 어떨까요? |
||
|
|
||
| return prev | ||
|
|
||
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/dolphinflow86.py
📊 시간/공간 복잡도 분석
피드백: 해당 구현은 모든 문자를 한 번씩 추가/제거하며 윈도우를 이동시키므로 선형 시간과 창 크기에 비례한 추가 공간을 사용한다.
개선 제안: 현재 구현이 적절해 보입니다.