diff --git a/coin-change/printjin-gmailcom.py b/coin-change/printjin-gmailcom.py new file mode 100644 index 000000000..ea63c675d --- /dev/null +++ b/coin-change/printjin-gmailcom.py @@ -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 diff --git a/find-minimum-in-rotated-sorted-array/printjin-gmailcom.py b/find-minimum-in-rotated-sorted-array/printjin-gmailcom.py new file mode 100644 index 000000000..bd71a4a6d --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/printjin-gmailcom.py @@ -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] diff --git a/maximum-depth-of-binary-tree/printjin-gmailcom.py b/maximum-depth-of-binary-tree/printjin-gmailcom.py new file mode 100644 index 000000000..8f190e858 --- /dev/null +++ b/maximum-depth-of-binary-tree/printjin-gmailcom.py @@ -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)) diff --git a/merge-two-sorted-lists/printjin-gmailcom.py b/merge-two-sorted-lists/printjin-gmailcom.py new file mode 100644 index 000000000..435fdda3c --- /dev/null +++ b/merge-two-sorted-lists/printjin-gmailcom.py @@ -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 diff --git a/word-search/printjin-gmailcom.py b/word-search/printjin-gmailcom.py new file mode 100644 index 000000000..1c742e07d --- /dev/null +++ b/word-search/printjin-gmailcom.py @@ -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