Skip to content

[devyejin] WEEK 04 solutions #1824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions coin-change/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# # 중복조합
# class Solution:
# def coinChange(self, coins: List[int], amount: int) -> int:
#
# def backtrack(current, total):
# if total == amount:
# return len(current)
#
# if total > amount:
# return float('inf')
#
# min_count = float('inf')
# for coin in coins:
# current.append(coin)
# result = backtrack(current, total + coin)
# min_count = min(min_count, result)
# current.pop()
#
# return min_count
#
# ans = backtrack([], 0)
# return -1 if ans == float('inf') else ans
from collections import deque
from typing import List

class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0

queue = deque([(0, 0)])
visited = set([0])

while queue:
current_amount, count = queue.popleft()

for coin in coins:
new_amount = current_amount + coin
if new_amount == amount:
return count + 1
if new_amount < amount and new_amount not in visited:
visited.add(new_amount)
queue.append((new_amount, count + 1))

return -1

3 changes: 3 additions & 0 deletions find-minimum-in-rotated-sorted-array/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
return min(nums)
12 changes: 12 additions & 0 deletions maximum-depth-of-binary-tree/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Definition for a binary tree node.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깊은 트리에서는 Python recursion limit 문제 발생 가능 → BFS나 stack 기반 DFS 방식도 고려하면 좋음.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

습관적으로 DFS 쓰게되는데 다른 방식 추천 감사합니다!

# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0

return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시잔&공간복잡도를 일관적으로 작성해보시는 것도 추천드립니다. 자꾸 하다보니 복잡도 계산이 전보다 쉬워지더라구요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞아요! 조언 감사합니다~ 앞으로 꾸준히 작성해볼게요 🥰

9 changes: 5 additions & 4 deletions maximum-subarray/devyejin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Solution:
def maxSubArray(self, nums: list[int]) -> int:
dp = [0] * len(nums)
dp[0] = nums[0]
current_sum = nums[0]
max_sum = nums[0]

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

return max(dp)
return max_sum

30 changes: 30 additions & 0 deletions merge-two-sorted-lists/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


"""
time : O(m+n)
space : O(1)
"""
from typing import Optional

class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()

node = dummy

while list1 and list2:
if list1.val <= list2.val:
node.next = list1
list1 = list1.next
else:
node.next = list2
list2 = list2.next
node = node.next
node.next = list1 or list2

return dummy.next
31 changes: 31 additions & 0 deletions word-search/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def exist(self, board: list[list[str]], word: str) -> bool:
def backtrack(x, y, idx):
if idx == len(word):
return True

if not (0 <= x < m and 0 <= y < n) or board[x][y] != word[idx]:
return False

tmp, board[x][y] = board[x][y], "@"

res = (
backtrack(x + 1, y, idx + 1) or
backtrack(x - 1, y, idx + 1) or
backtrack(x, y + 1, idx + 1) or
backtrack(x, y - 1, idx + 1)
)

board[x][y] = tmp
return res

m = len(board)
n = len(board[0])

for r in range(m):
for c in range(n):
if backtrack(r, c, 0):
return True

return False