Skip to content

Commit eaadebb

Browse files
authored
Merge pull request #2136 from ZetBe/main
[ZetBe] WEEK 04 solutions
2 parents e826ba4 + d63b4b7 commit eaadebb

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

β€Žcoin-change/ZetBe.pyβ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
문제: μ£Όμ–΄μ§„ 동전 μ’…λ₯˜λ‘œ νŠΉμ • κΈˆμ•‘μ„ λ§Œλ“€κΈ° μœ„ν•œ μ΅œμ†Œ 동전 개수λ₯Ό κ΅¬ν•˜μ‹œμ˜€.
3+
풀이: 동적 κ³„νšλ²•(DP)을 μ‚¬μš©ν•˜μ—¬ 각 κΈˆμ•‘μ— λŒ€ν•΄ μ΅œμ†Œ 동전 개수λ₯Ό κ³„μ‚°ν•©λ‹ˆλ‹€. λ§Œμ•½ νŠΉμ • κΈˆμ•‘μ„ λ§Œλ“€ 수 μ—†λ‹€λ©΄ -1을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
4+
μ‹œκ°„ λ³΅μž‘λ„: O(n * m), n은 κΈˆμ•‘(amount), m은 동전 μ’…λ₯˜μ˜ κ°œμˆ˜μž…λ‹ˆλ‹€. 각 κΈˆμ•‘μ— λŒ€ν•΄ λͺ¨λ“  동전을 ν™•μΈν•˜λ―€λ‘œ 전체 μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n * m)μž…λ‹ˆλ‹€.
5+
곡간 λ³΅μž‘λ„: O(n), κΈˆμ•‘(amount)κΉŒμ§€μ˜ μ΅œμ†Œ 동전 개수λ₯Ό μ €μž₯ν•˜λŠ” DP 배열을 μ‚¬μš©ν•˜λ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(n)μž…λ‹ˆλ‹€.
6+
μ‚¬μš©ν•œ 자료ꡬ쑰: λ°°μ—΄(DP λ°°μ—΄)
7+
'''
8+
9+
10+
class Solution:
11+
def coinChange(self, coins: List[int], amount: int) -> int:
12+
if amount == 0:
13+
return 0
14+
if len(coins) == 1 and coins[0] > amount:
15+
return -1
16+
if amount in coins:
17+
return 1
18+
19+
dp = [0 for i in range(amount+1)]
20+
21+
for i in range(amount+1):
22+
for j in range(len(coins)):
23+
if i == 0 and coins[j] < amount:
24+
dp[coins[j]] = 1
25+
elif i > 0 and 0 <= i+coins[j] <= amount:
26+
if dp[i] > 0 and dp[i+coins[j]] > 0:
27+
dp[i+coins[j]] = min(dp[i+coins[j]], dp[i]+1)
28+
elif dp[i] > 0 and dp[i+coins[j]] == 0:
29+
dp[i+coins[j]] = dp[i]+1
30+
31+
32+
33+
34+
if dp[amount] == 0:
35+
return -1
36+
return dp[amount]
37+
38+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Docstring for find-minimum-in-rotated-sorted-array.ZetBe
3+
문제: νšŒμ „λœ μ •λ ¬ λ°°μ—΄μ—μ„œ μ΅œμ†Ÿκ°’μ„ μ°ΎμœΌμ‹œμ˜€.
4+
풀이: 이진 탐색을 μ‚¬μš©ν•˜μ—¬ νšŒμ „λœ λ°°μ—΄μ—μ„œ μ΅œμ†Ÿκ°’μ„ 효율적으둜 μ°ΎμŠ΅λ‹ˆλ‹€.
5+
μ‹œκ°„ λ³΅μž‘λ„: O(log n), n은 λ°°μ—΄μ˜ κΈΈμ΄μž…λ‹ˆλ‹€. 이진 탐색을 μ‚¬μš©ν•˜μ—¬ μ ˆλ°˜μ”© 탐색 λ²”μœ„λ₯Ό μ€„μ΄λ―€λ‘œ 전체 μ‹œκ°„ λ³΅μž‘λ„λŠ” O(log n)μž…λ‹ˆλ‹€.
6+
곡간 λ³΅μž‘λ„: O(1), 좔가적인 곡간을 μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(1)μž…λ‹ˆλ‹€.
7+
μ‚¬μš©ν•œ 자료ꡬ쑰: λ°°μ—΄
8+
μΆ”κ°€λ‘œ, whileλ¬Έ λ‚΄λΆ€μ—μ„œ 무쑰건 λ¦¬ν„΄ν•˜κΈ° 떄문에, whileλ¬Έ 이후 λ„λ‹¬ν•˜λŠ” κ²½μš°λŠ” μ—†λ‹€.
9+
'''
10+
11+
12+
class Solution:
13+
def findMin(self, nums: List[int]) -> int:
14+
n = len(nums)
15+
if nums[0] <= nums[n-1]:
16+
return nums[0]
17+
18+
r, l = 0, n-1
19+
20+
while r < l:
21+
now = (r+l)//2
22+
if now < n-1 and nums[now] > nums[now+1]:
23+
return nums[now+1]
24+
25+
if now < n-1 and nums[0] > nums[now] <= nums[now+1]:
26+
l = now
27+
elif now < n-1 and nums[0] <= nums[now] <= nums[now+1]:
28+
r = now
29+
30+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
문제: 이진 트리의 μ΅œλŒ€ 깊이λ₯Ό κ΅¬ν•˜μ‹œμ˜€.
3+
풀이: 깊이 μš°μ„  탐색(DFS)을 μ‚¬μš©ν•˜μ—¬ 트리의 각 경둜λ₯Ό νƒμƒ‰ν•˜κ³ , μ΅œλŒ€ 깊이λ₯Ό κ°±μ‹ ν•©λ‹ˆλ‹€.
4+
μ‹œκ°„ λ³΅μž‘λ„: O(n), n은 트리의 λ…Έλ“œ μˆ˜μž…λ‹ˆλ‹€. λͺ¨λ“  λ…Έλ“œλ₯Ό ν•œ λ²ˆμ”© λ°©λ¬Έν•˜λ―€λ‘œ 전체 μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n)μž…λ‹ˆλ‹€.
5+
곡간 λ³΅μž‘λ„: O(h), hλŠ” 트리의 λ†’μ΄μž…λ‹ˆλ‹€. μž¬κ·€ 호좜 μŠ€νƒμ΄ μ΅œλŒ€ h κΉŠμ΄κΉŒμ§€ μŒ“μΌ 수 μžˆμœΌλ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(h)μž…λ‹ˆλ‹€.
6+
μ‚¬μš©ν•œ 자료ꡬ쑰: ν•¨μˆ˜(μž¬κ·€ 호좜 μŠ€νƒ)
7+
'''
8+
9+
10+
# Definition for a binary tree node.
11+
# class TreeNode:
12+
# def __init__(self, val=0, left=None, right=None):
13+
# self.val = val
14+
# self.left = left
15+
# self.right = right
16+
class Solution:
17+
def maxDepth(self, root: Optional[TreeNode]) -> int:
18+
if not root:
19+
return 0
20+
answ = 1
21+
def dfs(root, a):
22+
nonlocal answ
23+
if root.left:
24+
dfs(root.left, a+1)
25+
if root.right:
26+
dfs(root.right, a+1)
27+
answ = max(answ, a)
28+
return a
29+
dfs(root, 1)
30+
return answ
31+
32+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
문제: 두 개의 μ •λ ¬λœ μ—°κ²° 리슀트λ₯Ό λ³‘ν•©ν•˜μ—¬ ν•˜λ‚˜μ˜ μ •λ ¬λœ μ—°κ²° 리슀트λ₯Ό λ§Œλ“œμ‹œμ˜€.
3+
풀이: 두 μ—°κ²° 리슀트의 λ…Έλ“œλ₯Ό λΉ„κ΅ν•˜λ©΄μ„œ μž‘μ€ 값을 κ°€μ§„ λ…Έλ“œλ₯Ό κ²°κ³Ό λ¦¬μŠ€νŠΈμ— μΆ”κ°€ν•˜λŠ” λ°©μ‹μœΌλ‘œ λ³‘ν•©ν•©λ‹ˆλ‹€.
4+
μ‹œκ°„ λ³΅μž‘λ„: O(n + m), nκ³Ό m은 각각 두 μ—°κ²° 리슀트의 κΈΈμ΄μž…λ‹ˆλ‹€. 두 리슀트의 λͺ¨λ“  λ…Έλ“œλ₯Ό ν•œ λ²ˆμ”© λ°©λ¬Έν•˜λ―€λ‘œ 전체 μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n + m)μž…λ‹ˆλ‹€.
5+
곡간 λ³΅μž‘λ„: O(1), 좔가적인 μ—°κ²° 리슀트λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³  κΈ°μ‘΄ λ…Έλ“œλ“€μ„ μž¬μ‚¬μš©ν•˜λ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(1)μž…λ‹ˆλ‹€.
6+
μ‚¬μš©ν•œ 자료ꡬ쑰: μ—°κ²° 리슀트
7+
'''
8+
9+
10+
11+
# Definition for singly-linked list.
12+
# class ListNode:
13+
# def __init__(self, val=0, next=None):
14+
# self.val = val
15+
# self.next = next
16+
class Solution:
17+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
18+
if list2 == None:
19+
return list1
20+
if list1 == None:
21+
return list2
22+
23+
if list1.val <= list2.val:
24+
a, b = list1, list2
25+
else:
26+
b, a = list1, list2
27+
# 얕은 볡사 ν™œμš© (μ€‘μš”ν•΄λ³΄μž„)
28+
result = a
29+
while a != None and b != None:
30+
if a.next != None and a.val <= b.val and a.next.val <= b.val:
31+
a = a.next
32+
else:
33+
a.next, b = b, a.next
34+
a = a.next
35+
return result
36+
37+

β€Žword-search/ZetBe.pyβ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Docstring for word-search.ZetBe
3+
문제: 2D λ³΄λ“œμ—μ„œ 단어λ₯Ό μ°ΎμœΌμ‹œμ˜€.
4+
풀이: 깊이 μš°μ„  탐색(DFS)을 μ‚¬μš©ν•˜μ—¬ λ³΄λ“œμ˜ 각 μ…€μ—μ„œ μ‹œμž‘ν•˜μ—¬ 단어λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€. λ°©λ¬Έν•œ 셀은 λ‹€μ‹œ λ°©λ¬Έν•˜μ§€ μ•Šλ„λ‘ ν‘œμ‹œν•©λ‹ˆλ‹€.
5+
μ‹œκ°„ λ³΅μž‘λ„: O(m * n * 3^k), mκ³Ό n은 λ³΄λ“œμ˜ ν–‰κ³Ό μ—΄μ˜ 수, kλŠ” λ‹¨μ–΄μ˜ κΈΈμ΄μž…λ‹ˆλ‹€. 각 μ…€μ—μ„œ μ‹œμž‘ν•˜μ—¬ μ΅œλŒ€ 3κ°€μ§€ λ°©ν–₯으둜 탐색할 수 μžˆμœΌλ―€λ‘œ 전체 μ‹œκ°„ λ³΅μž‘λ„λŠ” O(m * n * 3^k)μž…λ‹ˆλ‹€.
6+
곡간 λ³΅μž‘λ„: O(k), kλŠ” λ‹¨μ–΄μ˜ κΈΈμ΄μž…λ‹ˆλ‹€. μž¬κ·€ 호좜 μŠ€νƒμ΄ μ΅œλŒ€ k κΉŠμ΄κΉŒμ§€ μŒ“μΌ 수 μžˆμœΌλ―€λ‘œ 곡간 λ³΅μž‘λ„λŠ” O(k)μž…λ‹ˆλ‹€.
7+
μ‚¬μš©ν•œ 자료ꡬ쑰: ν•¨μˆ˜(μž¬κ·€ 호좜 μŠ€νƒ), 2D 리슀트(λ°©λ¬Έ ν‘œμ‹œ)
8+
'''
9+
10+
class Solution:
11+
def exist(self, board: List[List[str]], word: str) -> bool:
12+
answ = False
13+
dx, dy = [0, 1, -1, 0], [1, 0, 0, -1]
14+
def dfs(x, y, i, v):
15+
nonlocal answ
16+
if i == len(word):
17+
answ = True
18+
return True
19+
if board[y][x] != word[i-1]:
20+
return False
21+
for j in range(4):
22+
ny, nx = y+dy[j], x+dx[j]
23+
if 0 <= ny < len(board) and 0 <= nx < len(board[0]) and v[ny][nx] == 0 and board[ny][nx] == word[i]:
24+
v[ny][nx] = 1
25+
dfs(nx, ny, i+1, v)
26+
v[ny][nx] = 0
27+
28+
for i in range(len(board)):
29+
for j in range(len(board[0])):
30+
v = [[0 for i in range(len(board[0]))] for j in range(len(board))]
31+
if board[i][j] == word[0]:
32+
v[i][j] = 1
33+
dfs(j, i, 1, v)
34+
35+
return answ
36+
37+

0 commit comments

Comments
Β (0)