From 00443608189aab2ee9670d51464cbb5da5c1c6ad Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Wed, 11 Nov 2026 19:34:45 +0900 Subject: [PATCH 1/8] Cotains Duplicate solution --- contains-duplicate/wlghsp.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 contains-duplicate/wlghsp.py diff --git a/contains-duplicate/wlghsp.py b/contains-duplicate/wlghsp.py new file mode 100644 index 0000000000..782253b835 --- /dev/null +++ b/contains-duplicate/wlghsp.py @@ -0,0 +1,7 @@ +""" + * Time Complexity: O(N) + * Space Complexity: O(N) +""" +class Solution: + def containsDuplicate(self, nums: list[int]) -> bool: + return len(set(nums)) != len(nums) \ No newline at end of file From b67fc36690ee62eabb2262dfe970384ffdbaf622 Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Wed, 11 Nov 2026 19:35:52 +0900 Subject: [PATCH 2/8] two sum solution --- two-sum/wlghsp.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 two-sum/wlghsp.py diff --git a/two-sum/wlghsp.py b/two-sum/wlghsp.py new file mode 100644 index 0000000000..6661995ddf --- /dev/null +++ b/two-sum/wlghsp.py @@ -0,0 +1,13 @@ +""" + * Time Complexity: O(N) + * Space Complexity: O(N) +""" +class Solution: + def twoSum(self, nums: list[int], target: int) -> list[int]: + num_dict = {} + for i, e in enumerate(nums): + other = target - e + if other in num_dict: + return [num_dict[other], i] + num_dict[e] = i + return [] \ No newline at end of file From 23c4d21a32fa3d68504c746c3b7d1cfeceda71cc Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Wed, 11 Nov 2026 19:36:07 +0900 Subject: [PATCH 3/8] top-k-frequent-elements solution --- top-k-frequent-elements/wlghsp.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 top-k-frequent-elements/wlghsp.py diff --git a/top-k-frequent-elements/wlghsp.py b/top-k-frequent-elements/wlghsp.py new file mode 100644 index 0000000000..0d8318970d --- /dev/null +++ b/top-k-frequent-elements/wlghsp.py @@ -0,0 +1,25 @@ +""" + * Time Complexity: O(n + ulogk) + * Space Complexity: O(n) +""" +import heapq + + +class Solution: + def topKFrequent(self, nums: list[int], k: int) -> list[int]: + nums_freq = {} + for n in nums: # n번 + if n in nums_freq: + nums_freq[n] += 1 + else: + nums_freq[n] = 1 + heap = [] + for num, freq in nums_freq.items(): # u번 (unique한 원소 갯수) + if len(heap) >= k: + if heap[0][0] < freq: + heapq.heappop(heap) + heapq.heappush(heap, (freq, num)) + else: + heapq.heappush(heap, (freq, num)) + return [k for _, k in heap] + \ No newline at end of file From 5ca735420add3662284d7b9d9c139e9501ef7b3d Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Thu, 12 Nov 2026 19:03:22 +0900 Subject: [PATCH 4/8] bucket sort solution --- top-k-frequent-elements/wlghsp.py | 39 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/top-k-frequent-elements/wlghsp.py b/top-k-frequent-elements/wlghsp.py index 0d8318970d..75edb6a64a 100644 --- a/top-k-frequent-elements/wlghsp.py +++ b/top-k-frequent-elements/wlghsp.py @@ -1,25 +1,24 @@ """ - * Time Complexity: O(n + ulogk) + * Time Complexity: O(n) * Space Complexity: O(n) """ -import heapq - - +from collections import Counter class Solution: def topKFrequent(self, nums: list[int], k: int) -> list[int]: - nums_freq = {} - for n in nums: # n번 - if n in nums_freq: - nums_freq[n] += 1 - else: - nums_freq[n] = 1 - heap = [] - for num, freq in nums_freq.items(): # u번 (unique한 원소 갯수) - if len(heap) >= k: - if heap[0][0] < freq: - heapq.heappop(heap) - heapq.heappush(heap, (freq, num)) - else: - heapq.heappush(heap, (freq, num)) - return [k for _, k in heap] - \ No newline at end of file + # 1. 빈도 계산: O(N) + count = Counter(nums) + + # 2. 빈도를 인덱스로 사용하는 버킷 배열 생성: O(N) + # 최대 빈도는 len(nums)이므로 크기는 len(nums) + 1 + buckets = [[] for _ in range(len(nums) + 1)] + for num, freq in count.items(): + buckets[freq].append(num) + + # 3. 높은 빈도(뒤쪽 인덱스)부터 k개 추출: O(N) + res = [] + for freq in range(len(buckets) - 1, 0, -1): + for num in buckets[freq]: + res.append(num) + if len(res) == k: + return res + return res From 308241de6e85144aae98adc0ed49f372d7bad784 Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Thu, 12 Nov 2026 19:04:29 +0900 Subject: [PATCH 5/8] longest-consecutive-sequence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hashset + 시작점 찾기 --- longest-consecutive-sequence/wlghsp.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 longest-consecutive-sequence/wlghsp.py diff --git a/longest-consecutive-sequence/wlghsp.py b/longest-consecutive-sequence/wlghsp.py new file mode 100644 index 0000000000..273a5f2825 --- /dev/null +++ b/longest-consecutive-sequence/wlghsp.py @@ -0,0 +1,15 @@ +""" + * Time Complexity: O(N) + * Space Complexity: O(N) +""" +class Solution: + def longestConsecutive(self, nums: list[int]) -> int: + nums_set = set(nums) + ans = 0 + for n in nums_set: + if (n - 1) not in nums_set: + length = 1 + while n + length in nums_set: + length += 1 + ans = max(length, ans) + return ans From e7439db451bf2722bbbad84c1a8733211e836d53 Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Fri, 13 Nov 2026 11:00:18 +0900 Subject: [PATCH 6/8] Fix missing newline at EOF --- contains-duplicate/wlghsp.py | 3 ++- two-sum/wlghsp.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/contains-duplicate/wlghsp.py b/contains-duplicate/wlghsp.py index 782253b835..c72f992599 100644 --- a/contains-duplicate/wlghsp.py +++ b/contains-duplicate/wlghsp.py @@ -4,4 +4,5 @@ """ class Solution: def containsDuplicate(self, nums: list[int]) -> bool: - return len(set(nums)) != len(nums) \ No newline at end of file + return len(set(nums)) != len(nums) + \ No newline at end of file diff --git a/two-sum/wlghsp.py b/two-sum/wlghsp.py index 6661995ddf..3e0787ef17 100644 --- a/two-sum/wlghsp.py +++ b/two-sum/wlghsp.py @@ -10,4 +10,5 @@ def twoSum(self, nums: list[int], target: int) -> list[int]: if other in num_dict: return [num_dict[other], i] num_dict[e] = i - return [] \ No newline at end of file + return [] + \ No newline at end of file From 360d8af9c3196f4788a8a4e20dd9f9ffb71d0a5e Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Fri, 13 Nov 2026 11:07:25 +0900 Subject: [PATCH 7/8] Remove trailing whitespace line at end of file --- contains-duplicate/wlghsp.py | 1 - two-sum/wlghsp.py | 1 - 2 files changed, 2 deletions(-) diff --git a/contains-duplicate/wlghsp.py b/contains-duplicate/wlghsp.py index c72f992599..7f23dd94f5 100644 --- a/contains-duplicate/wlghsp.py +++ b/contains-duplicate/wlghsp.py @@ -5,4 +5,3 @@ class Solution: def containsDuplicate(self, nums: list[int]) -> bool: return len(set(nums)) != len(nums) - \ No newline at end of file diff --git a/two-sum/wlghsp.py b/two-sum/wlghsp.py index 3e0787ef17..cbdd3fb7d3 100644 --- a/two-sum/wlghsp.py +++ b/two-sum/wlghsp.py @@ -11,4 +11,3 @@ def twoSum(self, nums: list[int], target: int) -> list[int]: return [num_dict[other], i] num_dict[e] = i return [] - \ No newline at end of file From 61fee064c45926acba363542e13eb302fd1d32f3 Mon Sep 17 00:00:00 2001 From: JIHO CHOI Date: Fri, 13 Nov 2026 15:15:17 +0900 Subject: [PATCH 8/8] house robber solution --- house-robber/wlghsp.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 house-robber/wlghsp.py diff --git a/house-robber/wlghsp.py b/house-robber/wlghsp.py new file mode 100644 index 0000000000..6d46f94cb0 --- /dev/null +++ b/house-robber/wlghsp.py @@ -0,0 +1,20 @@ +""" + * Time Complexity: O(N) + * Space Complexity: O(N) +""" +class Solution: + def rob(self, nums: list[int]) -> int: + n = len(nums) + + if n == 1: + return nums[0] + + # dp[i]: 0번 부터 i번째 집까지 고려했을 때 얻을 수 있는 최대 금액 + dp = [0] * n + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, n): + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) + + return dp[-1]