From 59c30ce7e40aca628a88c91473dcba373ff121bd Mon Sep 17 00:00:00 2001 From: Nayeon Date: Sun, 6 Apr 2025 15:21:18 +0900 Subject: [PATCH 1/5] Set up template for second solution --- top-k-frequent-elements/KwonNayeon.py | 24 ++++++++++------------- validate-binary-search-tree/KwonNayeon.py | 9 +++++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/top-k-frequent-elements/KwonNayeon.py b/top-k-frequent-elements/KwonNayeon.py index 4c11c5c9b..f5935e2ee 100644 --- a/top-k-frequent-elements/KwonNayeon.py +++ b/top-k-frequent-elements/KwonNayeon.py @@ -1,21 +1,17 @@ """ -Title: 237. Top K Frequent Elements -Link: https://leetcode.com/problems/top-k-frequent-elements/ - -Question: - - Given an integer array `nums` and an integer `k`, return the `k` most frequent elements. - - You may return the answer in any order. +Problem: 237. Top K Frequent Elements Constraints: - - 1 <= nums.length <= 10^5 - - -10^4 <= nums[i] <= 10^4 - - k is in the range [1, the number of unique elements in the array]. - - The answer is guaranteed to be unique. +- 1 <= nums.length <= 10^5 +- -10^4 <= nums[i] <= 10^4 +- k is in the range [1, the number of unique elements in the array]. +- The answer is guaranteed to be unique. + +Time Complexity: O(n log n) +- -Time Complexity: - - O(n log n) -Space Complexity: - - O(n) +Space Complexity: O(n) +- """ # Original Solution diff --git a/validate-binary-search-tree/KwonNayeon.py b/validate-binary-search-tree/KwonNayeon.py index 5535a5745..4f6e07a18 100644 --- a/validate-binary-search-tree/KwonNayeon.py +++ b/validate-binary-search-tree/KwonNayeon.py @@ -3,6 +3,7 @@ - The number of nodes in the tree is in the range [1, 10^4]. - -2^31 <= Node.val <= 2^31 - 1 + Time Complexity: O(n) - 트리의 모든 노드를 한 번씩 방문함 @@ -40,3 +41,11 @@ def validate(node, min_val, max_val): return validate(root, float("-inf"), float("inf")) +""" + +Time Complexity: +- + +Space Complexity: +- +""" From 9157bdb8d44e4429cb2168c6feba0d10cc93fdb1 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Sun, 6 Apr 2025 15:22:22 +0900 Subject: [PATCH 2/5] Set up template for second solution of "Validate Binary Search Tree" --- validate-binary-search-tree/KwonNayeon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/validate-binary-search-tree/KwonNayeon.py b/validate-binary-search-tree/KwonNayeon.py index 4f6e07a18..0a5be8f38 100644 --- a/validate-binary-search-tree/KwonNayeon.py +++ b/validate-binary-search-tree/KwonNayeon.py @@ -49,3 +49,4 @@ def validate(node, min_val, max_val): Space Complexity: - """ + From 2f629c2fac0bb6029d85d2dc461cb730bab215be Mon Sep 17 00:00:00 2001 From: Nayeon Date: Mon, 7 Apr 2025 10:45:53 +0900 Subject: [PATCH 3/5] Add detailed comments to Product of Array Except Self solution --- product-of-array-except-self/KwonNayeon.py | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/product-of-array-except-self/KwonNayeon.py b/product-of-array-except-self/KwonNayeon.py index 1ac91388e..659aae5fa 100644 --- a/product-of-array-except-self/KwonNayeon.py +++ b/product-of-array-except-self/KwonNayeon.py @@ -1,25 +1,32 @@ """ Constraints: - 1. 2 <= nums.length <= 10^5 - 2. -30 <= nums[i] <= 30 - 3. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer +- 나눗셈 연산 사용 불가능 +- O(n) 시간 복잡도로 구현해야 함 +- 모든 prefix/suffix 곱은 32비트 정수 범위 내에 있음 Time Complexity: O(n) - - 배열을 두 번 순회하므로 O(n) +- 배열을 두 번만 순회 Space Complexity: O(1) - - 출력 배열(answer)을 제외하면 추가 공간이 상수만큼만 필요(left, right 변수) +- 출력 배열 외 추가 공간은 상수만 사용 (left, right 변수) 풀이 방법: - 1. answer 배열을 1로 초기화 (곱셈에서는 1이 영향을 주지 않음) - 2. 왼쪽에서 오른쪽으로 순회: - - answer[i]에 현재까지의 left 누적값을 곱함 - - left *= nums[i]로 다음을 위해 left 값을 업데이트 - 3. 오른쪽에서 왼쪽으로 순회 (range(n-1, -1, -1) 사용): - - answer[i]에 현재까지의 right 누적값을 곱함 - - right *= nums[i]로 다음을 위해 right 값을 업데이트 +- 각 위치의 결과는 (왼쪽 모든 요소의 곱) * (오른쪽 모든 요소의 곱) +- 두 번의 순회로 이를 계산: + 1. 왼쪽 -> 오른쪽: 각 위치에 왼쪽 모든 요소의 누적 곱 저장 + 2. 오른쪽 -> 왼쪽: 각 위치에 오른쪽 모든 요소의 누적 곱을 곱함 + +예시: nums = [1, 2, 3, 4] +1단계 후: answer = [1, 1, 2, 6] +2단계 후: answer = [24, 12, 8, 6] """ +# Final result = (product of all elements on the left side) * (product of all elements on the right side) + +# Step 1: Initialize result array with 1s +# Step 2: Traverse from left to right, storing cumulative product of left side +# Step 3: Traverse from right to left, multiplying with cumulative product of right side +# nums = [1,2,3,4] class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: n = len(nums) @@ -29,10 +36,11 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: for i in range(n): answer[i] *= left left *= nums[i] - +# answer = [1, 1, 2, 6] at this point right = 1 for i in range(n-1, -1, -1): answer[i] *= right right *= nums[i] - + return answer +# answer = [24,12,8,6] From 5f75c017c3bfaf3ad3bdd28d662ff5a732ef1b19 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Tue, 8 Apr 2025 11:21:24 +0900 Subject: [PATCH 4/5] Add detailed comments to 3sum solution --- 3sum/KwonNayeon.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/3sum/KwonNayeon.py b/3sum/KwonNayeon.py index 3e5852dfb..e38d3bdcf 100644 --- a/3sum/KwonNayeon.py +++ b/3sum/KwonNayeon.py @@ -3,14 +3,35 @@ 1. 3 <= nums.length <= 3000 2. -10^5 <= nums[i] <= 10^5 -Time Complexity: - - O(n^2) (정렬은 O(n log n), 이중 반복문은 O(n^2)) -Space Complexity: - - O(n) (결과 리스트) +Time Complexity: O(n^2) +- 정렬은 O(n log n), 이중 반복문은 O(n^2) +Space Complexity: O(n) +- 결과 리스트를 저장하는 데 필요한 공간 + +풀이 방법: +- 투 포인터를 활용하여 합이 0이 되는 세 수 조합 찾기 +- 배열 정렬: 투 포인터 사용 + 중복 처리 용이 +- for 루프: 첫 번째 숫자 선택 (len(nums)-2까지) + - 중복된 첫 번째 숫자 건너뛰기 + - left, right 포인터 설정 +- while 루프: 두 포인터가 교차하지 않아야 함 + - sum = nums[i] + nums[left] + nums[right] 계산 + - sum == 0: 결과 추가, 중복 건너뛰기, 양쪽 포인터 이동 + - sum < 0: left 증가 (더 큰 값 필요) + - sum > 0: right 감소 (더 작은 값 필요) +- 최종 결과 반환 """ +# Brute-force: three nested loops → O(n^3) +# Optimized: sort + two pointer → O(n^2) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: + # Step 1: Sort the array + # Step 2: Fix one number using for loop + # Step 3: Use two pointers to find two other numbers + # - if sum == 0: valid triplet + # - if sum < 0: move left pointer + # - if sum > 0: move right pointer nums.sort() result = [] From e90447e48963c3446c211b606fb9cb10f3968cb1 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Wed, 9 Apr 2025 11:14:04 +0900 Subject: [PATCH 5/5] Add the second solution to Validate Binary Search Tree problem --- validate-binary-search-tree/KwonNayeon.py | 35 +++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/validate-binary-search-tree/KwonNayeon.py b/validate-binary-search-tree/KwonNayeon.py index 0a5be8f38..763dfa289 100644 --- a/validate-binary-search-tree/KwonNayeon.py +++ b/validate-binary-search-tree/KwonNayeon.py @@ -8,7 +8,7 @@ - 트리의 모든 노드를 한 번씩 방문함 Space Complexity: O(h) -- 재귀 호출 스택의 최대 깊이는 트리의 높이 +- 스택에는 최대 h개의 노드가 저장됨 (h는 트리의 높이) 풀이방법: 1. 각 노드가 가질 수 있는 값의 범위를 한정함 @@ -43,10 +43,35 @@ def validate(node, min_val, max_val): """ -Time Complexity: -- +Time Complexity: O(n) +- 트리의 모든 노드를 한 번씩 방문함 + +Space Complexity: O(h) +- 스택에는 최대 h개의 노드가 저장됨 (h는 트리의 높이) -Space Complexity: -- +노트: +- 재귀와 동일한 순서로 노드를 방문함 (왼쪽 서브트리 → 현재 노드 → 오른쪽 서브트리) +- 트리가 매우 깊어질 경우 발생할 수 있는 스택 오버플로우를 방지할 수 있음 """ +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + stack = [] + pre_val = float('-inf') + current = root + + while current or stack: + + while current: + stack.append(current) + current = current.left + + current = stack.pop() + + if current.val <= pre_val: + return False + + pre_val = current.val + current = current.right + + return True