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
16 changes: 16 additions & 0 deletions longest-substring-without-repeating-characters/daehyun99.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/daehyun99.py
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        l = 0
        seen = set()
        longest = 0

        for c in s:
            if c not in seen:
                seen.add(c)
                longest = max(longest, len(seen))
            else:
                while c in seen:
                    seen.remove(s[l])
                    l += 1
                seen.add(c)
        return longest
  • 패턴: Two Pointers, Hash Map / Hash Set, Sliding Window
  • 설명: 문자열에서 중복 문자를 제거하며 최장 부분 문자열 길이를 구하는 방식으로, 좌측 포인터와 우측 포인터처럼 창을 이동시키며 부분 문자열을 관리한다. 집합으로 현재 창의 문자를 추적하고 중복 시 창을 좁혀 재진입을 허용한다.

📊 시간/공간 복잡도 분석

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

피드백: seen 집합과 포인터 l을 사용해 중복 문자가 나오면 왼쪽을 제거하며 부분 문자열 길이를 갱신한다.

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

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

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:

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.

해당 로직이 하단의 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
26 changes: 26 additions & 0 deletions number-of-islands/daehyun99.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/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
  • 패턴: Depth-First Search, Backtracking
  • 설명: 섬의 연결 여부를 탐색하기 위해 스택으로 DFS 방식으로 인접 영역을 탐색합니다. 한 번 방문한 노드를 표시하여 전체 섬을 탐색하고 개수를 증가시키는 방식이 DFS 특징과 일치합니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(M * N) O(h * w)
Space O(M * N) O(h * w)

피드백: 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
20 changes: 20 additions & 0 deletions reverse-linked-list/daehyun99.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/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
  • 패턴: Two Pointers, Linked List
  • 설명: 주어진 코드는 단일 연결 리스트를 역방향으로 순회하며 포인터를 앞뒤로 바꿔 연결 방향을 뒤집는 구조로, 두 포인터를 활용한 순회 방식이 핵심 패턴입니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n)
Space O(1) 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.

해당 문제는 조금 더 코드 라인 수를 확 줄일수 있는 문제에요!
시도 한번 해보시는것도 좋겠어요!

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
22 changes: 22 additions & 0 deletions set-matrix-zeroes/daehyun99.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.

🏷️ 알고리즘 패턴 분석

set-matrix-zeroes/daehyun99.py
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
  • 패턴: Hash Map / Hash Set, Greedy
  • 설명: 행과 열의 제로 위치를 저장하기 위해 해시 세트를 사용하고, 그 정보를 바탕으로 행 전체를 0으로 만들고 나머지 열도 0으로 설정하는 방식으로 필요한 위치를 결정합니다. 직접적인 최적화보다는 저장 후 일괄 수정하는 패턴입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(m * n)
Space O(m + n)

피드백: 먼저 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

16 changes: 16 additions & 0 deletions unique-paths/daehyun99.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.

🏷️ 알고리즘 패턴 분석

unique-paths/daehyun99.py
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]
  • 패턴: Dynamic Programming
  • 설명: 2차원 DP 배열을 이용해 좌하에서 우상로의 경로 개수를 누적 합으로 구하는 전형적인 DP 문제 풀이이며, 이전 위치의 값을 활용해 현재 값을 계산한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(m * n)
Space O(m * n)

피드백: 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]
Loading