From 7fd6cd44efd46335709cba17c5b3c4841389837f Mon Sep 17 00:00:00 2001 From: changhyumm Date: Mon, 1 Dec 2025 23:57:53 +0900 Subject: [PATCH 1/6] merge-two-sorted-lists --- merge-two-sorted-lists/changhyumm.py | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 merge-two-sorted-lists/changhyumm.py diff --git a/merge-two-sorted-lists/changhyumm.py b/merge-two-sorted-lists/changhyumm.py new file mode 100644 index 0000000000..33eafe8a6c --- /dev/null +++ b/merge-two-sorted-lists/changhyumm.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 +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + # head of merged linked list를 반환해야 하므로 head를 임시로 생성 + head = ListNode() + pointer = head + + # 정렬된 링크드 리스트이므로 하나라도 끝까지 pointer를 이동해서 None이 될때까지 loop + # 시간복잡도 O(n), 공간복잡도 O(1) + while list1 and list2: + # val 비교해서 next 지정 + if list1.val <= list2.val: + pointer.next = list1 + list1 = list1.next + else: + pointer.next = list2 + list2 = list2.next + # pointer도 next로 이동 + pointer = pointer.next + # 남은 리스트의 경우 정렬되어 있으므로 그대로 연결 + if list1: + pointer.next = list1 + else: + pointer.next = list2 + + return head.next From 97c514d19e33b1a1f4dfda7365e375df0f524844 Mon Sep 17 00:00:00 2001 From: changhyumm Date: Thu, 4 Dec 2025 00:01:06 +0900 Subject: [PATCH 2/6] maximum-depth-of-binary-tree --- maximum-depth-of-binary-tree/changhyumm.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 maximum-depth-of-binary-tree/changhyumm.py diff --git a/maximum-depth-of-binary-tree/changhyumm.py b/maximum-depth-of-binary-tree/changhyumm.py new file mode 100644 index 0000000000..8ba1fe3d49 --- /dev/null +++ b/maximum-depth-of-binary-tree/changhyumm.py @@ -0,0 +1,20 @@ +# 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 root is None: + return 0 + stack = [[root, 1]] + max_depth = 1 + while stack: + node, cur_depth = stack.pop() + max_depth = max(max_depth, cur_depth) + if node.left: + stack.append([node.left, cur_depth + 1]) + if node.right: + stack.append([node.right, cur_depth + 1]) + return max_depth \ No newline at end of file From 49e7491fcf5d3a669656f7c97c1f6172ef2cc6fb Mon Sep 17 00:00:00 2001 From: changhyumm Date: Thu, 4 Dec 2025 00:26:22 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=EC=A3=BC=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maximum-depth-of-binary-tree/changhyumm.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maximum-depth-of-binary-tree/changhyumm.py b/maximum-depth-of-binary-tree/changhyumm.py index 8ba1fe3d49..770c0534ae 100644 --- a/maximum-depth-of-binary-tree/changhyumm.py +++ b/maximum-depth-of-binary-tree/changhyumm.py @@ -6,8 +6,11 @@ # self.right = right class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: + # root가 없는경우 depth 0 if root is None: return 0 + # depth를 같이 포함시킴 + # dfs의 경우 각 경로별로 끝까지 탐색하는데, 각 경로마다 depth가 다르므로 표기하면서 stack에 추가해야함 stack = [[root, 1]] max_depth = 1 while stack: From 967b3c3a1bacfd197309013805d87804689f1f0c Mon Sep 17 00:00:00 2001 From: changhyumm Date: Thu, 4 Dec 2025 22:33:39 +0900 Subject: [PATCH 4/6] minimum-in-rotated-sorted-array --- find-minimum-in-rotated-sorted-array/changhyumm.py | 14 ++++++++++++++ maximum-depth-of-binary-tree/changhyumm.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 find-minimum-in-rotated-sorted-array/changhyumm.py diff --git a/find-minimum-in-rotated-sorted-array/changhyumm.py b/find-minimum-in-rotated-sorted-array/changhyumm.py new file mode 100644 index 0000000000..640f045b91 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/changhyumm.py @@ -0,0 +1,14 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + # 시간복잡도 O(logn) 제약사항이 있으므로 binary search 사용 + left = 0 + right = len(nums) - 1 + # rotated sort 이므로 mid와 right 비교 + # mid 와 left 비교시 최소값이 어딨는지 특정할 수가 없음 (예외케이스 발생) + while left < right: + mid = (left + right) // 2 + if nums[mid] < nums[right]: + right = mid + else: + left = mid + 1 + return nums[left] diff --git a/maximum-depth-of-binary-tree/changhyumm.py b/maximum-depth-of-binary-tree/changhyumm.py index 770c0534ae..b37bd76a04 100644 --- a/maximum-depth-of-binary-tree/changhyumm.py +++ b/maximum-depth-of-binary-tree/changhyumm.py @@ -20,4 +20,4 @@ def maxDepth(self, root: Optional[TreeNode]) -> int: stack.append([node.left, cur_depth + 1]) if node.right: stack.append([node.right, cur_depth + 1]) - return max_depth \ No newline at end of file + return max_depth From 1465b01850d0c8bd1bf025b52f548ac9fdbd1eb1 Mon Sep 17 00:00:00 2001 From: changhyumm Date: Thu, 4 Dec 2025 23:42:09 +0900 Subject: [PATCH 5/6] word-search --- word-search/changhyumm.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 word-search/changhyumm.py diff --git a/word-search/changhyumm.py b/word-search/changhyumm.py new file mode 100644 index 0000000000..d807bd7c76 --- /dev/null +++ b/word-search/changhyumm.py @@ -0,0 +1,24 @@ +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + rows, cols = len(board), len(board[0]) + visited = set() + + def dfs(row, col, idx): + # 끝까지 탐색시 길이와 k가 같아지므로 True 반환 + if idx == len(word): + return True + # 범위를 벗어난 경우, 방문한 경우, 같은 word가 없는 경우 False 반환 + if not (0 <= row < rows) or not (0 <= col < cols) or (row, col) in visited or board[row][col] != word[idx]: + return False + + visited.add((row, col)) + res = dfs(row + 1, col, idx + 1) or dfs(row - 1, col, idx + 1) or dfs(row, col + 1, idx + 1) or dfs(row, col - 1, idx + 1) + visited.remove((row, col)) + return res + # 시작점 탐색 + for row in range(rows): + for col in range(cols): + if dfs(row, col, 0): + return True + # 시간복잡도 O(len(row)*len(col)*4^(word)) + return False From f0eab025423a64faf7c6105698d3841124026afa Mon Sep 17 00:00:00 2001 From: changhyumm Date: Thu, 4 Dec 2025 23:44:59 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=EC=A3=BC=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- word-search/changhyumm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/word-search/changhyumm.py b/word-search/changhyumm.py index d807bd7c76..c9ecd93999 100644 --- a/word-search/changhyumm.py +++ b/word-search/changhyumm.py @@ -20,5 +20,6 @@ def dfs(row, col, idx): for col in range(cols): if dfs(row, col, 0): return True - # 시간복잡도 O(len(row)*len(col)*4^(word)) + # 시간복잡도 O(rows*cols*4^(word)) + # 공간복잡도 O(rows*cols) return False