Skip to content

[yyyyyyyyyKim] WEEK 04 solutions #1349

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 7 commits into from
Apr 26, 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
20 changes: 20 additions & 0 deletions coin-change/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:

# DP
dp = [10001]*(amount+1)
dp[0] = 0

# 1부터 amount까지 만들 수 있는 최소 동전의 수를 dp에 업데이트
for i in range(1, amount+1):
for j in coins:
# dp[i-j]+1 : (i-j)원을 만드는 최소 동전 수 + 현재동전(j) 1개 사용
# 현재금액(i)를 만들 수 있는 최소 동전 수 업데이트
if i - j >= 0:
dp[i] = min(dp[i], dp[i-j]+1)

# 업데이트된 값이 없으면 -1 리턴
if dp[amount] == 10001:
return -1
else:
return dp[amount]
Copy link
Member

Choose a reason for hiding this comment

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

bottom up 방식으로 코드가 깔끔하게 작성되었네요👍
제 이전 풀이에서는 top down방식의 풀이로 진행해보니 코드가 장황하고 조건처리가 좀 많더라구요

24 changes: 24 additions & 0 deletions find-minimum-in-rotated-sorted-array/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
"""
시간복잡도: O(log n) - 이진 탐색
공간복잡도: O(1) - 추가 메모리 없음
"""

# 이진탐색
left = 0
right = len(nums) - 1

while left < right:
# 중간 인덱스
mid = (left+right)//2

# 최소값이 오른쪽에 있음
if nums[mid] > nums[right]:
left = mid + 1
# 최소값이 왼쪽(중간포함)에 있음
else:
right = mid

# 최종 최소값
return nums[left]
19 changes: 19 additions & 0 deletions maximum-depth-of-binary-tree/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for a binary tree node.
# 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:

# DFS, 재귀
def dfs(root):
# 노드가 없으면 깊이 0
if not root:
return 0

# 왼쪽과 오른쪽 중 더 깊은 쪽 + 1 리턴
return 1 + max(dfs(root.left), dfs(root.right))

return dfs(root)
32 changes: 32 additions & 0 deletions merge-two-sorted-lists/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:

dummy = ListNode(-1) # 더미 시작 노드
current = dummy # 현재 연결 위치 포인터

while list1 and list2:

# 현재list1의 값과 현재list2값을 비교해서 current.next 연결
if list1.val < list2.val:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next

# current 다음으로 이동
current = current.next

# 둘 중 하나가 남아있다면 나머지를 통째로 붙이기(삼항 연산자)
current.next = list1 if list1 else list2
# if list1:
# current.next = list1
# else:
# current.next = list2

return dummy.next
32 changes: 32 additions & 0 deletions word-search/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:

# 백트래킹, DFS, 재귀
def dfs(x, y, idx):
# 모든 글자를 다 찾았다면 True 리턴해서 종료
if idx == len(word):
return True

# 범위벗어나거나, 다른글자라면 False 리턴해서 종료(pruning가지치기)
if x < 0 or y < 0 or x >= len(board) or y >= len(board[0]) or board[x][y] != word[idx]:
return False

t = board[x][y] # 현재값 t에 임시저장
board[x][y] = ' ' # 방문표시

# 모든 방향(상하좌우) 탐색
for i, j in [(-1,0), (1,0), (0,-1), (0,1)]:
if dfs(x+i, y+j, idx+1):
return True

board[x][y] = t # 방문복원

return False


for i in range(len(board)):
for j in range(len(board[0])):
if dfs(i, j, 0):
return True

return False