From 9ccd081719dbef0ccbf0c0aaa722f0c534de02bc Mon Sep 17 00:00:00 2001 From: thispath98 Date: Thu, 20 Feb 2025 16:31:44 +0900 Subject: [PATCH 1/4] feat: Add Reorder List solution --- reorder-list/thispath98.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 reorder-list/thispath98.py diff --git a/reorder-list/thispath98.py b/reorder-list/thispath98.py new file mode 100644 index 000000000..ec8a8cfeb --- /dev/null +++ b/reorder-list/thispath98.py @@ -0,0 +1,31 @@ +class Solution: + def reorderList(self, head: Optional[ListNode]) -> None: + """ + Intuition: + nodes 리스트에 모든 노드를 저장한다. + 현재 노드가 -i - 1번째 노드일 때, + 현재 노드의 next는 head의 next로 설정하고, + head의 next는 현재 노드로 설정한다. + 이후 마지막에 중간에 있는 노드를 None으로 설정한다. + + Time Complexity: + O(N): + 모든 노드를 순회하므로 O(N)이다. + + Space Complexity: + O(N): + 모든 노드를 nodes 리스트에 저장하므로 O(N)이다. + """ + nodes = [] + node = head + while node: + nodes.append(node) + node = node.next + + for i in range((len(nodes) - 1) // 2): + cur = nodes[-i - 1] + cur.next = head.next + head.next = cur + head = cur.next + + nodes[len(nodes) // 2].next = None From 959c10c5646004f9e5424212517a58867ccc6aea Mon Sep 17 00:00:00 2001 From: thispath98 Date: Thu, 20 Feb 2025 16:33:36 +0900 Subject: [PATCH 2/4] feat: Maximum Depth of Binary Tree solution --- maximum-depth-of-binary-tree/thispath98.py | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 maximum-depth-of-binary-tree/thispath98.py diff --git a/maximum-depth-of-binary-tree/thispath98.py b/maximum-depth-of-binary-tree/thispath98.py new file mode 100644 index 000000000..48ab7361d --- /dev/null +++ b/maximum-depth-of-binary-tree/thispath98.py @@ -0,0 +1,25 @@ +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + """ + Intuition: + 왼쪽 자식과 오른쪽 자식에 대해 depth를 증가시키며 재귀하고, + 둘 중 큰 값을 반환한다. + + Time Complexity: + O(N): + 모든 노드에 대해 재귀적으로 호출하므로 O(N)이다. + + Space Complexity: + O(1): + answer 변수만 사용하므로 O(1)이다. + """ + def get_depth(node, depth): + if not node: + return depth + + left = get_depth(node.left, depth + 1) + right = get_depth(node.right, depth + 1) + return max(left, right) + + answer = get_depth(root, 0) + return answer From 2e059f52f2ad4aeaf60a557cbe387ac0d2410b99 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Sat, 22 Feb 2025 15:02:16 +0900 Subject: [PATCH 3/4] feat: add Merge Intervals solutions --- merge-intervals/thispath98.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 merge-intervals/thispath98.py diff --git a/merge-intervals/thispath98.py b/merge-intervals/thispath98.py new file mode 100644 index 000000000..ac7ce9b98 --- /dev/null +++ b/merge-intervals/thispath98.py @@ -0,0 +1,28 @@ +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + """ + Intuition: + 정렬된 intervals을 순회하면서 이전의 인터벌과 현재 인터벌이 + 겹친다면, 이를 갱신해준다. + 겹치지 않는다면, 새로운 인터벌을 추가한다. + + Time Complexity: + O(N log N): + 최초에 정렬을 하므로, O(N log N)이다. + 이후 한번 스캔을 하므로 O(N)이다. + 따라서 시간 복잡도는 O(N log N)이다. + + Space Complexity: + O(N): + answer에 N개의 값을 저장하므로 O(N)이다. + """ + sorted_intervals = sorted(intervals) + + answer = [sorted_intervals[0]] + for start, end in sorted_intervals[1:]: + prev_start, prev_end = answer[-1] + if prev_end >= start: + answer[-1][1] = max(prev_end, end) + else: + answer.append([start, end]) + return answer From 269516b1e21eb272d030ed1039ee28d36178449b Mon Sep 17 00:00:00 2001 From: thispath98 Date: Sat, 22 Feb 2025 15:06:03 +0900 Subject: [PATCH 4/4] feat: fix Space Complexity for Maximum Depth of Binary Tree solution --- maximum-depth-of-binary-tree/thispath98.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/maximum-depth-of-binary-tree/thispath98.py b/maximum-depth-of-binary-tree/thispath98.py index 48ab7361d..47bd85a12 100644 --- a/maximum-depth-of-binary-tree/thispath98.py +++ b/maximum-depth-of-binary-tree/thispath98.py @@ -10,9 +10,11 @@ def maxDepth(self, root: Optional[TreeNode]) -> int: 모든 노드에 대해 재귀적으로 호출하므로 O(N)이다. Space Complexity: - O(1): - answer 변수만 사용하므로 O(1)이다. + O(h): + 트리의 높이 h만큼 재귀 함수를 호출하므로, + 공간 복잡도는 O(h)이다. """ + def get_depth(node, depth): if not node: return depth @@ -20,6 +22,6 @@ def get_depth(node, depth): left = get_depth(node.left, depth + 1) right = get_depth(node.right, depth + 1) return max(left, right) - + answer = get_depth(root, 0) return answer