-
-
Notifications
You must be signed in to change notification settings - Fork 195
[printjin-gmailcom] Week 4 Solutions #1331
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
6 commits
Select commit
Hold shift + click to select a range
a604816
Solve : Maximum Depth of Binary Tree - BFS
printjin-gmailcom 35d7816
Solve : Maximum Depth of Binary Tree - DFS
printjin-gmailcom c0feba2
Solve : Find Minimum In Rotated Sorted Array
printjin-gmailcom 19d1776
Solve : Word Search
printjin-gmailcom 904339d
Solve : Coin Change
printjin-gmailcom 783de6a
Solve : Merge Two Sorted Lists
printjin-gmailcom 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,8 @@ | ||
class Solution: | ||
def coinChange(self, coins, amount): | ||
dp = [float('inf')] * (amount + 1) | ||
dp[0] = 0 | ||
for coin in coins: | ||
for x in range(coin, amount + 1): | ||
dp[x] = min(dp[x], dp[x - coin] + 1) | ||
return dp[amount] if dp[amount] != float('inf') else -1 |
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,12 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def findMin(self, nums): | ||
left, right = 0, 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,12 @@ | ||
# 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: | ||
if not root: | ||
return 0 | ||
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) |
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 @@ | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
class Solution: | ||
def mergeTwoLists(self, list1, list2): | ||
dummy = ListNode() | ||
current = dummy | ||
while list1 and list2: | ||
if list1.val < list2.val: | ||
current.next = list1 | ||
list1 = list1.next | ||
else: | ||
current.next = list2 | ||
list2 = list2.next | ||
current = current.next | ||
current.next = list1 if list1 else 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,23 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def exist(self, board, word): | ||
m, n = len(board), len(board[0]) | ||
def dfs(x, y, index): | ||
if index == len(word): | ||
return True | ||
if x < 0 or x >= m or y < 0 or y >= n or board[x][y] != word[index]: | ||
return False | ||
temp = board[x][y] | ||
board[x][y] = '#' | ||
found = ( | ||
dfs(x + 1, y, index + 1) or dfs(x - 1, y, index + 1) or dfs(x, y + 1, index + 1) or dfs(x, y - 1, index + 1) | ||
) | ||
board[x][y] = temp | ||
return found | ||
|
||
for i in range(m): | ||
for j in range(n): | ||
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.
한줄에 or 로 하니 보기 더 좋네요