Skip to content

Commit 23cbc9b

Browse files
authored
Merge pull request #1824 from devyejin/main
[devyejin] WEEK 04 solutions
2 parents 152774e + f93ae6c commit 23cbc9b

File tree

6 files changed

+127
-4
lines changed

6 files changed

+127
-4
lines changed

coin-change/devyejin.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# # 중복조합
2+
# class Solution:
3+
# def coinChange(self, coins: List[int], amount: int) -> int:
4+
#
5+
# def backtrack(current, total):
6+
# if total == amount:
7+
# return len(current)
8+
#
9+
# if total > amount:
10+
# return float('inf')
11+
#
12+
# min_count = float('inf')
13+
# for coin in coins:
14+
# current.append(coin)
15+
# result = backtrack(current, total + coin)
16+
# min_count = min(min_count, result)
17+
# current.pop()
18+
#
19+
# return min_count
20+
#
21+
# ans = backtrack([], 0)
22+
# return -1 if ans == float('inf') else ans
23+
from collections import deque
24+
from typing import List
25+
26+
class Solution:
27+
def coinChange(self, coins: List[int], amount: int) -> int:
28+
if amount == 0:
29+
return 0
30+
31+
queue = deque([(0, 0)])
32+
visited = set([0])
33+
34+
while queue:
35+
current_amount, count = queue.popleft()
36+
37+
for coin in coins:
38+
new_amount = current_amount + coin
39+
if new_amount == amount:
40+
return count + 1
41+
if new_amount < amount and new_amount not in visited:
42+
visited.add(new_amount)
43+
queue.append((new_amount, count + 1))
44+
45+
return -1
46+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
return min(nums)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def maxDepth(self, root: Optional[TreeNode]) -> int:
9+
if not root:
10+
return 0
11+
12+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

maximum-subarray/devyejin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution:
22
def maxSubArray(self, nums: list[int]) -> int:
3-
dp = [0] * len(nums)
4-
dp[0] = nums[0]
3+
current_sum = nums[0]
4+
max_sum = nums[0]
55

66
for i in range(1, len(nums)):
7-
dp[i] = max(nums[i], dp[i - 1] + nums[i])
7+
current_sum = max(nums[i], current_sum + nums[i])
8+
max_sum = max(current_sum, max_sum)
89

9-
return max(dp)
10+
return max_sum
1011

merge-two-sorted-lists/devyejin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
7+
8+
"""
9+
time : O(m+n)
10+
space : O(1)
11+
"""
12+
from typing import Optional
13+
14+
class Solution:
15+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
16+
dummy = ListNode()
17+
18+
node = dummy
19+
20+
while list1 and list2:
21+
if list1.val <= list2.val:
22+
node.next = list1
23+
list1 = list1.next
24+
else:
25+
node.next = list2
26+
list2 = list2.next
27+
node = node.next
28+
node.next = list1 or list2
29+
30+
return dummy.next

word-search/devyejin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def exist(self, board: list[list[str]], word: str) -> bool:
3+
def backtrack(x, y, idx):
4+
if idx == len(word):
5+
return True
6+
7+
if not (0 <= x < m and 0 <= y < n) or board[x][y] != word[idx]:
8+
return False
9+
10+
tmp, board[x][y] = board[x][y], "@"
11+
12+
res = (
13+
backtrack(x + 1, y, idx + 1) or
14+
backtrack(x - 1, y, idx + 1) or
15+
backtrack(x, y + 1, idx + 1) or
16+
backtrack(x, y - 1, idx + 1)
17+
)
18+
19+
board[x][y] = tmp
20+
return res
21+
22+
m = len(board)
23+
n = len(board[0])
24+
25+
for r in range(m):
26+
for c in range(n):
27+
if backtrack(r, c, 0):
28+
return True
29+
30+
return False
31+

0 commit comments

Comments
 (0)