Skip to content

Commit 461271f

Browse files
authored
Merge pull request #2472 from gcount85/main
2 parents 88b9d51 + afdd5ed commit 461271f

5 files changed

Lines changed: 147 additions & 0 deletions

File tree

coin-change/gcount85.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
# Intuition
3+
dp[n]은 n원을 만드는데 필요한 최소 동전 개수
4+
e.g. n = 11일 때, 11원을 만들기 위한 최소 동전 개수는 11에서 coins의 원소를 뺀 dp 값 + 1이다.
5+
6+
# Complexity
7+
- Time complexity: O(amount * coins.length)인데, coins 배열 길이가 상수라서 무시 => O(amount)
8+
9+
- Space complexity: dp 배열 생성으로 O(amount)
10+
"""
11+
12+
13+
class Solution:
14+
def coinChange(self, coins: list[int], amount: int) -> int:
15+
coins.sort()
16+
INF = float("inf")
17+
dp = [INF] * (amount + 1)
18+
dp[0] = 0
19+
for i in range(amount + 1):
20+
if dp[i] == INF:
21+
continue
22+
for c in coins:
23+
if i + c > amount:
24+
break
25+
dp[i + c] = min(dp[i + c], dp[i] + 1)
26+
return dp[-1] if dp[-1] != INF else -1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Intuition
3+
이진탐색으로 찾는다.
4+
5+
# Complexity
6+
- Time complexity: nums의 길이 N일때, O(logN)
7+
8+
- Space complexity: 재귀스택 O(logN)
9+
"""
10+
11+
12+
class Solution:
13+
def findMin(self, nums: list[int]) -> int:
14+
l, m, r = 0, len(nums) // 2, len(nums) - 1
15+
16+
def findMinimum(l, m, r):
17+
if l == m or r == m:
18+
return min(nums[l], nums[r])
19+
20+
# 오른쪽 구간
21+
if nums[m] > nums[r]:
22+
return findMinimum(m, (m + r) // 2, r)
23+
24+
# 왼쪽 구간
25+
return findMinimum(l, (l + m) // 2, m)
26+
27+
return findMinimum(l, m, r)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
# Intuition
3+
1) BFS + 큐
4+
2) DFS + 재귀
5+
1번으로 풀었다가 2번이 코드가 더 간결해서 바꿨습니다.
6+
7+
# Complexity
8+
- Time complexity: 모든 노드를 다 봐야 하므로 O(N)
9+
10+
- Space complexity: 재귀 깊이만큼 O(H)
11+
"""
12+
13+
14+
class Solution:
15+
def maxDepth(self, root: Optional[TreeNode]) -> int:
16+
if not root:
17+
return 0
18+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

merge-two-sorted-lists/gcount85.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
# Intuition
3+
각각의 리스트의 첫 노드를 가리키면서 병합 리스트를 생성한다.
4+
5+
# Complexity
6+
- Time complexity: list1과 list2의 길이 N, M일때 O(N+M)
7+
8+
- Space complexity: 기존 노드 연결만 바꿈 O(1)
9+
"""
10+
11+
12+
class Solution:
13+
def mergeTwoLists(
14+
self, list1: Optional[ListNode], list2: Optional[ListNode]
15+
) -> Optional[ListNode]:
16+
dummy = ListNode(0)
17+
tail = dummy
18+
19+
while list1 and list2:
20+
if list1.val <= list2.val:
21+
tail.next = list1
22+
list1 = list1.next
23+
else:
24+
tail.next = list2
25+
list2 = list2.next
26+
tail = tail.next
27+
28+
# 남은 쪽 붙이기
29+
if list1:
30+
tail.next = list1
31+
else:
32+
tail.next = list2
33+
34+
return dummy.next

word-search/gcount85.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
# Intuition
3+
백트래킹으로 풀었습니다.
4+
5+
# Complexity
6+
- Time complexity: O(M*N*3^L) => M*N 보드 한 칸을 모두 방문하며,
7+
word 의 길이(L) 횟수 단계 만큼 탐색해야 한다. 매 단계 탐색할 때는 매번 3갈래씩 길을 찾는다.
8+
9+
- Space complexity: 재귀 스택 O(L), visited 크기 O(L)
10+
"""
11+
12+
13+
class Solution:
14+
def exist(self, board: list[list[str]], word: str) -> bool:
15+
m, n = len(board[0]), len(board)
16+
direction = [[0, 1], [0, -1], [1, 0], [-1, 0]]
17+
18+
def dfs(x, y, cur_step, visited):
19+
if cur_step == len(word) - 1:
20+
return True
21+
22+
for dx, dy in direction:
23+
nx, ny = x + dx, y + dy
24+
if nx < 0 or ny < 0 or nx >= n or ny >= m:
25+
continue
26+
if board[nx][ny] != word[cur_step + 1]:
27+
continue
28+
if (nx, ny) in visited:
29+
continue
30+
visited.add((nx, ny))
31+
if dfs(nx, ny, cur_step + 1, visited):
32+
return True
33+
visited.discard((nx, ny))
34+
return False
35+
36+
for x, row in enumerate(board):
37+
for y, start_word in enumerate(row):
38+
if word[0] != start_word:
39+
continue
40+
if dfs(x, y, 0, {(x, y)}):
41+
return True
42+
return False

0 commit comments

Comments
 (0)