Skip to content

Commit 4189053

Browse files
authored
Merge pull request #2124 from ppxyn1/main
[ppxyn1] WEEK 04 solutions
2 parents 1d57850 + cb01505 commit 4189053

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

coin-change/ppxyn1.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# idea: DFS/BFS, DP
2+
3+
from collections import deque
4+
class Solution:
5+
def coinChange(self, coins: List[int], amount: int) -> int:
6+
# Why Greedy is not possiple way?
7+
# A greedy is only optimal in specific coin systems (e.g., denominations like 1, 5, 10, 25)
8+
# For arbitrary coin denominations, a greedy approach does not always yield the optimal solution.
9+
queue = deque([(0,0)]) # (동전갯수, 누적금액)
10+
while queue:
11+
count, total = queue.popleft()
12+
if total == amount:
13+
return count
14+
for coin in coins:
15+
if total + coin <= amount:
16+
queue.append([count+1, total+ coin])
17+
return -1
18+
19+
# # BFS
20+
# def coinChange(self, coins: List[int], amount: int) -> int:
21+
# queue = deque([(0,0)]) # (동전갯수, 누적금액)
22+
# visited = set()
23+
# while queue:
24+
# count, total = queue.popleft()
25+
# if total == amount:
26+
# return count
27+
# if total in visited:
28+
# continue
29+
# visited.add(total)
30+
# for coin in coins:
31+
# if total + coin <= amount:
32+
# queue.append([count+1, total+ coin])
33+
# return -1
34+
35+
36+
# DP
37+
# dp[i] = min(dp[i], dp[i-coin]+1)
38+
# from collections import deque
39+
# class Solution:
40+
# def coinChange(self, coins: List[int], amount: int) -> int:
41+
# dp=[0]+[amount+1]*amount
42+
# for coin in coins:
43+
# for i in range(coin, amount+1):
44+
# dp[i] = min(dp[i], dp[i-coin]+1)
45+
# return dp[amount] if dp[amount] < amount else -1
46+
47+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
# You must write an algorithm that runs in O(log n) time. >> Binary Search
4+
# It's possible because the array is sorted.
5+
left, right = 0, len(nums)-1
6+
7+
while left < right:
8+
mid = (left + right) // 2
9+
# If the value at mid is greater than the value at the right end, it means the minimum place to the right of mid.
10+
if nums[mid] > nums[right]:
11+
left = mid + 1
12+
else:
13+
# mid can be minimum number
14+
# But is it possible to solve it with "right=mid-1"? e.g,[4,5,6,7,0,1,2]
15+
right = mid
16+
return nums[left]
17+
18+
19+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# idea : DFS
2+
3+
# time : O(n)
4+
# space : O(n)
5+
6+
class Solution:
7+
def maxDepth(self, root: Optional[TreeNode]) -> int:
8+
# edge case
9+
if not root:
10+
return 0
11+
max_depth = 0
12+
stack = [(root, 1)]
13+
while stack:
14+
node, depth = stack.pop()
15+
max_depth = max(depth, max_depth)
16+
if node.left:
17+
stack.append((node.left, depth+1))
18+
if node.right:
19+
stack.append((node.right, depth+1))
20+
return max_depth
21+
22+
23+
24+
25+
# another way : Down-top : recursive
26+
# class Solution:
27+
# def maxDepth(self, root: Optional[TreeNode]) -> int:
28+
# if not root:
29+
# return 0
30+
# return 1 + max(
31+
# self.maxDepth(root.left),
32+
# self.maxDepth(root.right)
33+
# )
34+
35+
36+

merge-two-sorted-lists/ppxyn1.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# idea : Two Pointer
2+
# It was first time solving a two-pointer problem using nodes instead of arrays, and I realized I'm still not very familiar with nodes yet.
3+
class Solution:
4+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
5+
dummy = ListNode(None)
6+
node = dummy
7+
while list1 and list2:
8+
if list1.val < list2.val:
9+
node.next = list1
10+
list1 = list1.next
11+
else:
12+
node.next = list2
13+
list2 = list2.next
14+
node = node.next
15+
node.next = list1 or list2
16+
return dummy.next
17+
18+
19+
# Another way to solve it
20+
# class Solution:
21+
# def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
22+
# if not (list1 and list2):
23+
# return list1 or list2
24+
# if list1.val < list2.val:
25+
# list1.next = self.mergeTwoLists(list1.next, list2)
26+
# return list1
27+
# else:
28+
# list2.next = self.mergeTwoLists(list1, list2.next)
29+
# return list2
30+
31+

word-search/ppxyn1.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# idea : dfs
2+
class Solution:
3+
def exist(self, board: List[List[str]], word: str) -> bool:
4+
ROWS, COLS = len(board), len(board[0])
5+
visited = [[False]*COLS for _ in range(ROWS)]
6+
7+
def dfs(r, c, index):
8+
if r < 0 or r >= ROWS or c < 0 or c >= COLS:
9+
return False
10+
if visited[r][c] or board[r][c] != word[index]:
11+
return False
12+
13+
if index == len(word) - 1:
14+
return True
15+
16+
visited[r][c] = True
17+
18+
found = (dfs(r+1, c, index+1) or
19+
dfs(r-1, c, index+1) or
20+
dfs(r, c+1, index+1) or
21+
dfs(r, c-1, index+1))
22+
23+
# For next backtracking
24+
visited[r][c] = False
25+
26+
return found
27+
28+
# Run DFS from every cell as a starting point
29+
for row in range(ROWS):
30+
for col in range(COLS):
31+
if dfs(row, col, 0):
32+
return True
33+
34+
return False
35+
36+
37+

0 commit comments

Comments
 (0)