Skip to content

Commit 62beb78

Browse files
authored
Merge pull request #2127 from daiyongg-kim/main
[daiyongg-kim] WEEK 04 solutions
2 parents 7ad1a91 + be7f1df commit 62beb78

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

coin-change/daiyongg-kim.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
if amount == 0:
4+
return 0
5+
6+
level = [0]
7+
visited = {0}
8+
count = 0
9+
10+
while level:
11+
count += 1
12+
next_level = []
13+
14+
for current_amount in level:
15+
for coin in coins:
16+
new_amount = current_amount + coin
17+
18+
if new_amount == amount:
19+
return count
20+
21+
if new_amount < amount and new_amount not in visited:
22+
visited.add(new_amount)
23+
next_level.append(new_amount)
24+
level = next_level
25+
26+
return -1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
#constraint complexity: O(log n)
4+
left = 0
5+
right = len(nums) - 1
6+
7+
while left < right:
8+
mid = (left + right) // 2
9+
10+
if nums[mid] < nums[right]:
11+
right = mid
12+
else:
13+
left = mid + 1
14+
return nums[left]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
3+
def maxDepth(self, root: Optional[TreeNode]) -> int:
4+
if not root:
5+
return 0
6+
queue = deque([root])
7+
depth = 0
8+
9+
while queue:
10+
depth += 1
11+
print(len(queue))
12+
for _ in range(len(queue)):
13+
node = queue.popleft()
14+
if node.left:
15+
queue.append(node.left)
16+
if node.right:
17+
queue.append(node.right)
18+
return depth
19+
20+
21+
# if not root:
22+
# return 0
23+
24+
# left_depth = self.maxDepth(root.left)
25+
# right_depth = self.maxDepth(root.right)
26+
27+
# return max(left_depth, right_depth) + 1
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
if list1 is None:
9+
return list2
10+
if list2 is None:
11+
return list1
12+
13+
if list1.val < list2.val:
14+
list1.next = self.mergeTwoLists(list1.next, list2)
15+
return list1
16+
else:
17+
list2.next = self.mergeTwoLists(list1, list2.next)
18+
return list2
19+
20+
# class Solution:
21+
# def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
22+
# result = ListNode(-1)
23+
# dummy = result
24+
# while list1 and list2:
25+
# if list1.val < list2.val:
26+
# dummy.next = list1
27+
# list1 = list1.next
28+
# else:
29+
# dummy.next = list2
30+
# list2 = list2.next
31+
# dummy = dummy.next
32+
33+
# if list1:
34+
# dummy.next = list1
35+
# else:
36+
# dummy.next = list2
37+
38+
# return result.next

word-search/daiyongg-kim.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
3+
def exist(self, board: List[List[str]], word: str) -> bool:
4+
row = len(board)
5+
col = len(board[0])
6+
7+
def dfs(i: int, j: int, k: int):
8+
if k == len(word):
9+
return True
10+
11+
if i < 0 or i >= row or j < 0 or j >= col or board[i][j] != word[k]:
12+
return False
13+
14+
current = board[i][j]
15+
board[i][j] = ''
16+
17+
if dfs(i-1, j, k+1) or dfs(i+1, j, k+1) or dfs(i, j-1, k+1) or dfs(i, j+1, k+1):
18+
return True
19+
20+
board[i][j] = current
21+
return False
22+
23+
for i in range(row):
24+
for j in range(col):
25+
if dfs(i, j, 0):
26+
return True
27+
28+
return False

0 commit comments

Comments
 (0)