-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
624f908
merge-two-sorted-lists solution
yyyyyyyyyKim a3f69e6
merge-two-sorted-lists solution
yyyyyyyyyKim 54fcea1
maximum-depth-of-binary-tree solution
yyyyyyyyyKim 3da533f
find-minimum-in-rotated-sorted-array solution
yyyyyyyyyKim 37b3797
word-search solution
yyyyyyyyyKim e7edab1
word-search solution
yyyyyyyyyKim 761312e
coin-change solution
yyyyyyyyyKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bottom up 방식으로 코드가 깔끔하게 작성되었네요👍
제 이전 풀이에서는 top down방식의 풀이로 진행해보니 코드가 장황하고 조건처리가 좀 많더라구요