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
18 changes: 18 additions & 0 deletions coin-change/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
N: amount
M: coins 개수
TC: O(N * M)
SC: O(N)

"""
class Solution:
def coinChange(self, coins: list[int], amount: int) -> int:
dp = [float('inf')] * (amount + 1)
dp[0] = 0

for i in range(1, amount + 1):
for coin in coins:
if i - coin >= 0:
dp[i] = min(dp[i], dp[i - coin] + 1)

return dp[amount] if dp[amount] != float('inf') else - 1
25 changes: 25 additions & 0 deletions find-minimum-in-rotated-sorted-array/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
TC: O(log N)
SC: O(1)

N: 배열 nums의 원소 개수

- 매 반복마다 탐색 범위를 절반으로 줄이므로 O(log N)
- 별도의 자료구조나 재귀 호출을 사용하지 않으므로 O(1)
"""
class Solution:
def findMin(self, nums: list[int]) -> int:
left, right = 0, len(nums) - 1

while left < right:
mid = (left + right) // 2

if nums[mid] > nums[right]:
left = mid + 1

else:
right = mid

return nums[left]


21 changes: 21 additions & 0 deletions maximum-depth-of-binary-tree/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
N: 트리의 전체 노드 개수
H: 트리의 높이
TC : O(N)
- DFS를 통해 모든 노드를 한 번씩 방문한다
- 각 노드에서 수행하는 연산은 O(1)이다.
- 따라서 전체 시간 복잡도는 O(N)이다.
SC: O(H) (최악의 경우 O(N))
- 별도의 자료구조를 생성하지 않지만, 재귀 호출로 인해 Call Stack(호출 스택) 공간이 필요하다

"""
class Solution:
def maxDepth(self, root: TreeNode | None) -> int:
if not root:
return 0
left = self.maxDepth(root.left)

right = self.maxDepth(root.right)

return max(left, right) + 1

30 changes: 30 additions & 0 deletions merge-two-sorted-lists/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
N: list1의 노드 개수
M: list2의 노드 개수

TC : O(N + M)
- 두 연결 리스트의 노드를 비교하면서 순차적으로 탐색한다
- 최악의 경우 두 리스트의 전체 노드 개수에 비례하여 연산을 수행한다
SC: O(1)
- 기존 노드를 재사용하여 연결하므로 별도의 리스트를 생성하지 않는다
- dummy 노드와 current 포인터 등 상수 크기의 추가 공간만 사용한다

"""
class Solution:
def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListNode | None:
dummy = ListNode()
current = dummy

while list1 and list2:
if list1.val < list2.val:
current.next = list1
list1.next = list1
else:
current.next = list2
list2.next = list2

current = current.next

current.next = list1 if list1 else list2

return dummy.next
47 changes: 47 additions & 0 deletions word-search/wlghsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
M: 행의 개수
N: 열의 개수
L: 단어의 길이
TC: O(M * N * 3^L)
SC: O(L)
"""
class Solution:
def exist(self, board: list[list[str]], word: str) -> bool:
M = len(board)
N = len(board[0])
L = len(word)

def dfs(x, y, index):
if index == L:
return True

if not (0 <= x < M and 0 <= y < N):
return False
if board[x][y] != word[index]:
return False

# 기존 문자 저장하고 방문 처리
temp = board[x][y]
board[x][y] = '#'

found = False
# found = (dfs(x + 1, y, index + 1) or dfs(x - 1, y, index + 1)
# or dfs(x, y + 1, index + 1) or dfs(x, y - 1, index + 1))
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
nx, ny = x + dx, y + dy
if dfs(nx, ny, index + 1):
found = True
break

# 기존 문자 복구
board[x][y] = temp

Comment on lines +37 to +39

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

저는 stack으로 풀어서 경로를 다 본 뒤 복구하려고 복구 항목을 따로 넣었는데, 재귀로 푸니까 호출이 끝나고 돌아온 시점에 바로 복구할 수 있어서 더 직관적이네요!

return found

for i in range(M):
for j in range(N):
if dfs(i, j, 0):
return True

return False
Loading