Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions longest-substring-without-repeating-characters/dolphinflow86.py

Copy link
Copy Markdown
Contributor

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
# 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

            chars.add(char)
            longest = max(longest, right - left + 1)

        return longest
  • 패턴: Sliding Window, Hash Map / Hash Set
  • 설명: 문자열에서 중복 제거를 위해 창을 움직이며(left, right) 현재 윈도우의 문자들을 집합에 저장하고, 중복 시 왼쪽 포인터를 이동시키는 슬라이딩 윈도우 패턴을 사용합니다. 해시 세트를 이용해 문자 존재 여부를 확인합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(min(n, m))

피드백: 해당 구현은 모든 문자를 한 번씩 추가/제거하며 윈도우를 이동시키므로 선형 시간과 창 크기에 비례한 추가 공간을 사용한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left를 하나씩 증가하지 않고 각 문자의 인덱스를 저장해서 점프하는 방식으로도 조금 더 최적화가 가능하니 풀어보셔도 좋을 거 같습니다.


chars.add(char)
longest = max(longest, right - left + 1)

return longest

36 changes: 36 additions & 0 deletions number-of-islands/dolphinflow86.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
  • 패턴: Depth-First Search, Backtracking
  • 설명: 그리드에서 1로 연결된 영역을 DFS로 탐색하며 방문한 노드를 0으로 바꿔 연결 요소(섬)의 개수를 셈. 재귀를 이용한 깊은 탐색이 핵심 패턴입니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(R * C) O(rows * cols)
Space O(R * C) O(rows * cols)

피드백: 그리드의 각 셀을 한 번씩 방문하고 인접한 '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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

@parkhojeong parkhojeong Aug 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python의 a <= x < b 쓰면 이런 형태도 가능한데 or 사용해주신게 더 명확한 거 같기는 하네요.

if not (
    0 <= row < rows 
    and 0 <= col < cols 
    and 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

16 changes: 16 additions & 0 deletions reverse-linked-list/dolphinflow86.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
  • 패턴: Two Pointers, Linked List
  • 설명: 주어진 코드는 단순히 포인터 두 개를 사용해 링크드 리스트의 방향을 반대로 바꾸는 과정으로, 노드를 순회하며 노드의 링크를 뒤집는 데 두 포인터를 활용하는 패턴이 핵심입니다. 시간 복잡도 O(N), 공간 복잡도 O(1)로 구현됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 연결 구조를 역전시키면서 상호 참조를 유지하는 표준 패턴이다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제 개인적인 생각에는요! head를 리턴 혹은 어디에도 쓰지 않으니 current를 쓰시는 부분 그대로 head를 쓰셔도 되지 않을까?
하는 생각이 들어요!

그리고 파이썬의 a, b = b, a처럼 쓸수 있는 문법을 활용하시면 while loop 안을 1줄로 줄이실수도 있답니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next가 예약어여서 _node를 붙여주신 거 같네요. 다른 네이밍들이랑 일관성을 맞추시는 건 어떨까요?


return prev

Loading