From a604816b1981d14ec0669c87b7f5a4a9dc23dee2 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 20 Apr 2025 10:10:58 +0900 Subject: [PATCH 1/6] Solve : Maximum Depth of Binary Tree - BFS --- .../printjin-gmailcom.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 maximum-depth-of-binary-tree/printjin-gmailcom.py 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..d60fe6fb6 --- /dev/null +++ b/maximum-depth-of-binary-tree/printjin-gmailcom.py @@ -0,0 +1,20 @@ +from collections import deque + +class Solution: + def maxDepth(self, root): + if not root: + return 0 + + queue = deque([root]) + depth = 0 + + while queue: + for _ in range(len(queue)): + node = queue.popleft() + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + depth += 1 + + return depth From 35d78167f9fc1761a9721ac05caa98862337b957 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 20 Apr 2025 10:11:33 +0900 Subject: [PATCH 2/6] Solve : Maximum Depth of Binary Tree - DFS --- .../printjin-gmailcom.py | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/maximum-depth-of-binary-tree/printjin-gmailcom.py b/maximum-depth-of-binary-tree/printjin-gmailcom.py index d60fe6fb6..8f190e858 100644 --- a/maximum-depth-of-binary-tree/printjin-gmailcom.py +++ b/maximum-depth-of-binary-tree/printjin-gmailcom.py @@ -1,20 +1,12 @@ -from collections import deque +# 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): + def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 - - queue = deque([root]) - depth = 0 - - while queue: - for _ in range(len(queue)): - node = queue.popleft() - if node.left: - queue.append(node.left) - if node.right: - queue.append(node.right) - depth += 1 - - return depth + return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) From c0feba26ce798ccac1b46ce1d61bacfc317f1f8a Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 20 Apr 2025 13:22:02 +0900 Subject: [PATCH 3/6] Solve : Find Minimum In Rotated Sorted Array --- .../printjin-gmailcom.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/printjin-gmailcom.py 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] From 19d177642d9e5bc885fa57bc78ef5921d59f3b85 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 20 Apr 2025 18:39:01 +0900 Subject: [PATCH 4/6] Solve : Word Search --- word-search/printjin-gmailcom.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 word-search/printjin-gmailcom.py 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 From 904339d0567507da8200535b403f31ab81a7ce62 Mon Sep 17 00:00:00 2001 From: printjin Date: Mon, 21 Apr 2025 08:03:55 +0900 Subject: [PATCH 5/6] Solve : Coin Change --- coin-change/printjin-gmailcom.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 coin-change/printjin-gmailcom.py 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 From 783de6a839ef0372c813e255fbb42e1276c569c5 Mon Sep 17 00:00:00 2001 From: printjin Date: Mon, 21 Apr 2025 08:04:36 +0900 Subject: [PATCH 6/6] Solve : Merge Two Sorted Lists --- merge-two-sorted-lists/printjin-gmailcom.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 merge-two-sorted-lists/printjin-gmailcom.py 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