Skip to content

[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 6 commits into from
Apr 21, 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
8 changes: 8 additions & 0 deletions coin-change/printjin-gmailcom.py
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
12 changes: 12 additions & 0 deletions find-minimum-in-rotated-sorted-array/printjin-gmailcom.py
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]
12 changes: 12 additions & 0 deletions maximum-depth-of-binary-tree/printjin-gmailcom.py
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))
19 changes: 19 additions & 0 deletions merge-two-sorted-lists/printjin-gmailcom.py
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
23 changes: 23 additions & 0 deletions word-search/printjin-gmailcom.py
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)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

한줄에 or 로 하니 보기 더 좋네요

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