From ab025f25cf4745663352f3eded79e80a9683a88a Mon Sep 17 00:00:00 2001 From: hyedall Date: Tue, 1 Dec 2026 23:02:28 +0900 Subject: [PATCH 1/6] merge two sorted lists solution --- merge-two-sorted-lists/hyedall.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 merge-two-sorted-lists/hyedall.py diff --git a/merge-two-sorted-lists/hyedall.py b/merge-two-sorted-lists/hyedall.py new file mode 100644 index 0000000000..07c85ffc47 --- /dev/null +++ b/merge-two-sorted-lists/hyedall.py @@ -0,0 +1,29 @@ +""" +https://leetcode.com/problems/merge-two-sorted-lists/description/ + +시간 복잡도 : O(n) +공간 복잡도 : O(1) +""" + +# 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: ListNode | None, list2: ListNode | None) -> ListNode | None: + + dummy = ListNode(0) + tail = dummy + + while list1 and list2: + if list1.val <= list2.val: + tail.next = list1 + list1 = list1.next + else: + tail.next = list2 + list2 = list2.next + + tail = tail.next + + tail.next = list1 if list1 else list2 From 73e03b7e01c7758bcee49c0a6c89d3a69b526ab9 Mon Sep 17 00:00:00 2001 From: hyedall Date: Tue, 1 Dec 2026 23:27:39 +0900 Subject: [PATCH 2/6] maximum-depth-of-binary-tree solution --- maximum-depth-of-binary-tree/hyedall.py | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maximum-depth-of-binary-tree/hyedall.py diff --git a/maximum-depth-of-binary-tree/hyedall.py b/maximum-depth-of-binary-tree/hyedall.py new file mode 100644 index 0000000000..14d24dedcc --- /dev/null +++ b/maximum-depth-of-binary-tree/hyedall.py @@ -0,0 +1,31 @@ +""" +# https://leetcode.com/problems/maximum-depth-of-binary-tree/ + +시간 복잡도 : O(n) +공간 복잡도 : O(n) +""" + +# 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: TreeNode | None) -> int: + if not root: + return 0 + + answer = 0 + stack = [(root, 1)] + + while stack: + cur, depth = stack.pop() + answer = max(answer, depth) + + if cur.left: + stack.append((cur.left, depth+1)) + if cur.right: + stack.append((cur.right, depth+1)) + + return answer From f62e7aabf4d5323b0064b5a19d7796a053744412 Mon Sep 17 00:00:00 2001 From: hyedall Date: Sat, 5 Dec 2026 22:36:59 +0900 Subject: [PATCH 3/6] word-search solution --- word-search/hyedall.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 word-search/hyedall.py diff --git a/word-search/hyedall.py b/word-search/hyedall.py new file mode 100644 index 0000000000..2978638c6d --- /dev/null +++ b/word-search/hyedall.py @@ -0,0 +1,44 @@ +""" +https://leetcode.com/problems/word-search/description/ + +시간 복잡도 : O(m * n * 3^L * L) +공간 복잡도 : O(m * n + L^2) +""" + +class Solution: + def exist(self, board: list[list[str]], word: str) -> bool: + n, m = len(board[0]), len(board) + + # 상, 하, 좌, 우 + xx = [0, 0, -1, 1] + xy = [-1, 1, 0, 0] + + stack = [] + + # 시작점 탐색 + for i in range(n): + for j in range(m): + if board[j][i] == word[0]: + stack.append((i, j, 0, {(i, j)})) + + while stack: + x, y, index, visited = stack.pop() + + if index == len(word) - 1: + return True + + for i in range(4): + nx = x + xx[i] + ny = y + xy[i] + + if (0 <= nx < n) and (0 <= ny < m): + if (nx, ny) not in visited: + if board[ny][nx] == word[index + 1]: + stack.append(( + nx, + ny, + index + 1, + visited | {(nx, ny)} + )) + + return False \ No newline at end of file From 121b7ac6a557fe931cdaee75c61bc1df0b2a3b46 Mon Sep 17 00:00:00 2001 From: hyedall Date: Sat, 5 Dec 2026 22:58:51 +0900 Subject: [PATCH 4/6] fix: add missing return statement in mergeTwoLists --- merge-two-sorted-lists/hyedall.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/merge-two-sorted-lists/hyedall.py b/merge-two-sorted-lists/hyedall.py index 07c85ffc47..55b4d4e002 100644 --- a/merge-two-sorted-lists/hyedall.py +++ b/merge-two-sorted-lists/hyedall.py @@ -27,3 +27,5 @@ def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListN tail = tail.next tail.next = list1 if list1 else list2 + + return dummy.next From 4b488eb5f8e437789ff83ae0d61c76dc7b68b981 Mon Sep 17 00:00:00 2001 From: hyedall Date: Sat, 5 Dec 2026 23:06:45 +0900 Subject: [PATCH 5/6] fix: add final newline and address review feedback --- word-search/hyedall.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/word-search/hyedall.py b/word-search/hyedall.py index 2978638c6d..45f3e70c11 100644 --- a/word-search/hyedall.py +++ b/word-search/hyedall.py @@ -10,8 +10,7 @@ def exist(self, board: list[list[str]], word: str) -> bool: n, m = len(board[0]), len(board) # 상, 하, 좌, 우 - xx = [0, 0, -1, 1] - xy = [-1, 1, 0, 0] + directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] stack = [] @@ -27,9 +26,9 @@ def exist(self, board: list[list[str]], word: str) -> bool: if index == len(word) - 1: return True - for i in range(4): - nx = x + xx[i] - ny = y + xy[i] + for dx, dy in directions: + nx = x + dx + ny = y + dy if (0 <= nx < n) and (0 <= ny < m): if (nx, ny) not in visited: @@ -41,4 +40,4 @@ def exist(self, board: list[list[str]], word: str) -> bool: visited | {(nx, ny)} )) - return False \ No newline at end of file + return False From b3cf7407549538e2a86e96527b5b82a90a00538e Mon Sep 17 00:00:00 2001 From: hyedall Date: Sat, 5 Dec 2026 23:11:26 +0900 Subject: [PATCH 6/6] refactor: simplify remaining list assignment --- merge-two-sorted-lists/hyedall.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge-two-sorted-lists/hyedall.py b/merge-two-sorted-lists/hyedall.py index 55b4d4e002..2b88ef0152 100644 --- a/merge-two-sorted-lists/hyedall.py +++ b/merge-two-sorted-lists/hyedall.py @@ -26,6 +26,6 @@ def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListN tail = tail.next - tail.next = list1 if list1 else list2 + tail.next = list1 or list2 return dummy.next