From 855e2fc181d0bfd7bcc712f9624d09b1ec50edce Mon Sep 17 00:00:00 2001 From: yejin Date: Wed, 13 Aug 2025 21:57:15 +0900 Subject: [PATCH 1/9] maximum depth of binary tree solution --- maximum-depth-of-binary-tree/devyejin.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 maximum-depth-of-binary-tree/devyejin.py diff --git a/maximum-depth-of-binary-tree/devyejin.py b/maximum-depth-of-binary-tree/devyejin.py new file mode 100644 index 0000000000..201bb16146 --- /dev/null +++ b/maximum-depth-of-binary-tree/devyejin.py @@ -0,0 +1,15 @@ +# 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 + + left_max_dept = self.maxDepth(root.left) + 1 + right_max_dept = self.maxDepth(root.right) + 1 + + return max(left_max_dept, right_max_dept) \ No newline at end of file From 7fd579d3d884289476fd8c2a6d4b69c0dd5e0ada Mon Sep 17 00:00:00 2001 From: yejin Date: Wed, 13 Aug 2025 22:35:19 +0900 Subject: [PATCH 2/9] =?UTF-8?q?maxDepth=20=EA=B3=84=EC=82=B0=20=EB=8B=A8?= =?UTF-8?q?=EC=88=9C=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maximum-depth-of-binary-tree/devyejin.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/maximum-depth-of-binary-tree/devyejin.py b/maximum-depth-of-binary-tree/devyejin.py index 201bb16146..7eecbb1247 100644 --- a/maximum-depth-of-binary-tree/devyejin.py +++ b/maximum-depth-of-binary-tree/devyejin.py @@ -9,7 +9,4 @@ def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 - left_max_dept = self.maxDepth(root.left) + 1 - right_max_dept = self.maxDepth(root.right) + 1 - - return max(left_max_dept, right_max_dept) \ No newline at end of file + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 From 3bdc80f801bd55a0092a003cd9ae7dba80dd2531 Mon Sep 17 00:00:00 2001 From: yejin Date: Thu, 14 Aug 2025 00:38:32 +0900 Subject: [PATCH 3/9] merge two sorted lists solution --- merge-two-sorted-lists/devyejin.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 merge-two-sorted-lists/devyejin.py diff --git a/merge-two-sorted-lists/devyejin.py b/merge-two-sorted-lists/devyejin.py new file mode 100644 index 0000000000..0e2cdb29ad --- /dev/null +++ b/merge-two-sorted-lists/devyejin.py @@ -0,0 +1,30 @@ +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +""" +time : O(m+n) +space : O(1) +""" + + +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(None) + + node = dummy + + while list1 and list2: + if list1.val <= list2.val: + node.next = list1 + list1 = list1.next + else: + node.next = list2 + list2 = list2.next + node = node.next + node.next = list1 or list2 + + return dummy.next From 065acb26460c7829942774b9b02a724753132f04 Mon Sep 17 00:00:00 2001 From: yejin Date: Thu, 14 Aug 2025 10:47:38 +0900 Subject: [PATCH 4/9] =?UTF-8?q?maximum=20subarray=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20:=20=20=EA=B3=B5=EA=B0=84=20=EC=B5=9C?= =?UTF-8?q?=EC=A0=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maximum-subarray/devyejin.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maximum-subarray/devyejin.py b/maximum-subarray/devyejin.py index 1d0ddeb820..b6003864cd 100644 --- a/maximum-subarray/devyejin.py +++ b/maximum-subarray/devyejin.py @@ -1,10 +1,11 @@ class Solution: def maxSubArray(self, nums: list[int]) -> int: - dp = [0] * len(nums) - dp[0] = nums[0] + current_sum = nums[0] + max_sum = nums[0] for i in range(1, len(nums)): - dp[i] = max(nums[i], dp[i - 1] + nums[i]) + current_sum = max(nums[i], current_sum + nums[i]) + max_sum = max(current_sum, max_sum) - return max(dp) + return max_sum From 4bd55ba40d021912a86478c52604b06841df462f Mon Sep 17 00:00:00 2001 From: yejin Date: Sat, 16 Aug 2025 22:13:30 +0900 Subject: [PATCH 5/9] word search solution --- word-search/devyejin.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 word-search/devyejin.py diff --git a/word-search/devyejin.py b/word-search/devyejin.py new file mode 100644 index 0000000000..c6b5e08fe8 --- /dev/null +++ b/word-search/devyejin.py @@ -0,0 +1,30 @@ +class Solution: + def exist(self, board: list[list[str]], word: str) -> bool: + def backtrack(x, y, idx): + if idx == len(word): + return True + + if not (0 <= x < m and 0 <= y < n) or board[x][y] != word[idx]: + return False + + tmp, board[x][y] = board[x][y], "@" + + res = ( + backtrack(x + 1, y, idx + 1) or + backtrack(x - 1, y, idx + 1) or + backtrack(x, y + 1, idx + 1) or + backtrack(x, y - 1, idx + 1) + ) + + board[x][y] = tmp + return res + + m = len(board) + n = len(board[0]) + + for r in range(m): + for c in range(n): + if backtrack(r, c, 0): + return True + + return False From 80955d91664d38328d554ee9c289f57cc4f6765a Mon Sep 17 00:00:00 2001 From: yejin Date: Sun, 17 Aug 2025 01:34:18 +0900 Subject: [PATCH 6/9] find minimum in rotated sorted array solution --- find-minimum-in-rotated-sorted-array/devyejin.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/devyejin.py diff --git a/find-minimum-in-rotated-sorted-array/devyejin.py b/find-minimum-in-rotated-sorted-array/devyejin.py new file mode 100644 index 0000000000..5476270e89 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/devyejin.py @@ -0,0 +1,3 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + return min(nums) \ No newline at end of file From 56cd3244254d7884e86afcd51b00b7e77f8e816d Mon Sep 17 00:00:00 2001 From: yejin Date: Mon, 18 Aug 2025 00:10:38 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=EC=A4=84=EB=B0=94=EA=BF=88=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- find-minimum-in-rotated-sorted-array/devyejin.py | 2 +- word-search/devyejin.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/find-minimum-in-rotated-sorted-array/devyejin.py b/find-minimum-in-rotated-sorted-array/devyejin.py index 5476270e89..4ff70cfd43 100644 --- a/find-minimum-in-rotated-sorted-array/devyejin.py +++ b/find-minimum-in-rotated-sorted-array/devyejin.py @@ -1,3 +1,3 @@ class Solution: def findMin(self, nums: List[int]) -> int: - return min(nums) \ No newline at end of file + return min(nums) diff --git a/word-search/devyejin.py b/word-search/devyejin.py index c6b5e08fe8..e5f349cdbf 100644 --- a/word-search/devyejin.py +++ b/word-search/devyejin.py @@ -28,3 +28,4 @@ def backtrack(x, y, idx): return True return False + From 08cfbf7360404f24c70c31d31a2b6b7093d8b937 Mon Sep 17 00:00:00 2001 From: yejin Date: Mon, 18 Aug 2025 01:21:26 +0900 Subject: [PATCH 8/9] coin change solution --- coin-change/devyejin.py | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 coin-change/devyejin.py diff --git a/coin-change/devyejin.py b/coin-change/devyejin.py new file mode 100644 index 0000000000..f725a2fd6a --- /dev/null +++ b/coin-change/devyejin.py @@ -0,0 +1,46 @@ +# # 중복조합 +# class Solution: +# def coinChange(self, coins: List[int], amount: int) -> int: +# +# def backtrack(current, total): +# if total == amount: +# return len(current) +# +# if total > amount: +# return float('inf') +# +# min_count = float('inf') +# for coin in coins: +# current.append(coin) +# result = backtrack(current, total + coin) +# min_count = min(min_count, result) +# current.pop() +# +# return min_count +# +# ans = backtrack([], 0) +# return -1 if ans == float('inf') else ans +from collections import deque +from typing import List + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + if amount == 0: + return 0 + + queue = deque([(0, 0)]) + visited = set([0]) + + while queue: + current_amount, count = queue.popleft() + + for coin in coins: + new_amount = current_amount + coin + if new_amount == amount: + return count + 1 + if new_amount < amount and new_amount not in visited: + visited.add(new_amount) + queue.append((new_amount, count + 1)) + + return -1 + From f93ae6c75f825fa5a76cf5500bdb87889e57bb47 Mon Sep 17 00:00:00 2001 From: yejin Date: Mon, 18 Aug 2025 01:45:31 +0900 Subject: [PATCH 9/9] =?UTF-8?q?fix=20:=20=20=ED=83=80=EC=9E=85=20=EC=9D=BC?= =?UTF-8?q?=EA=B4=80=EC=84=B1=20=EC=9C=A0=EC=A7=80,=20ListNode=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=8B=9C=20None=20=EB=8C=80=EC=8B=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=EA=B0=92=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- merge-two-sorted-lists/devyejin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/merge-two-sorted-lists/devyejin.py b/merge-two-sorted-lists/devyejin.py index 0e2cdb29ad..b8e848ec1c 100644 --- a/merge-two-sorted-lists/devyejin.py +++ b/merge-two-sorted-lists/devyejin.py @@ -9,11 +9,11 @@ def __init__(self, val=0, next=None): time : O(m+n) space : O(1) """ - +from typing import Optional class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: - dummy = ListNode(None) + dummy = ListNode() node = dummy