From 9b63b0ec14d19644284b78d14adca386ea402885 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Mon, 9 Nov 2026 23:56:26 +0900 Subject: [PATCH 01/15] contains duplicate solution --- contains-duplicate/201411108.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 contains-duplicate/201411108.py diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py new file mode 100644 index 0000000000..d5026c90fa --- /dev/null +++ b/contains-duplicate/201411108.py @@ -0,0 +1,9 @@ +class Solution: + def containsDuplicate(self, nums: list[int]) -> bool: + numSet = set(nums) + + if (len(numSet) == len(nums)): + return False + + return True + \ No newline at end of file From ded82283632f2c7a232169f7af21d06b847add11 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Tue, 10 Nov 2026 00:08:26 +0900 Subject: [PATCH 02/15] add complexity comment --- contains-duplicate/201411108.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py index d5026c90fa..d3ddef60ef 100644 --- a/contains-duplicate/201411108.py +++ b/contains-duplicate/201411108.py @@ -1,3 +1,5 @@ +# TC: O(n), nums의 원소를 하나씩 확인하기 때문에 O(n) +# SC: O(n), 최악의 경우 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) class Solution: def containsDuplicate(self, nums: list[int]) -> bool: numSet = set(nums) From dd453852376737fc99de8735b70518e23c4b6cab Mon Sep 17 00:00:00 2001 From: 201411108 Date: Tue, 10 Nov 2026 00:12:50 +0900 Subject: [PATCH 03/15] improve time complexity --- contains-duplicate/201411108.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py index d3ddef60ef..51cf024f91 100644 --- a/contains-duplicate/201411108.py +++ b/contains-duplicate/201411108.py @@ -1,11 +1,13 @@ -# TC: O(n), nums의 원소를 하나씩 확인하기 때문에 O(n) +# TC: O(n), nums의 원소를 하나씩 확인하기 때문에 O(n), 첫 두 원소가 같다면 O(1) # SC: O(n), 최악의 경우 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) class Solution: def containsDuplicate(self, nums: list[int]) -> bool: - numSet = set(nums) + unique = set() - if (len(numSet) == len(nums)): - return False + for num in nums: + if nums in unique: + return True + unique.add(num) return True \ No newline at end of file From 9708e211f00032cd716f60e3dfda372ccd4a0ef4 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Tue, 10 Nov 2026 00:18:21 +0900 Subject: [PATCH 04/15] fix typo and add comments --- contains-duplicate/201411108.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py index 51cf024f91..83bf519def 100644 --- a/contains-duplicate/201411108.py +++ b/contains-duplicate/201411108.py @@ -1,13 +1,12 @@ # TC: O(n), nums의 원소를 하나씩 확인하기 때문에 O(n), 첫 두 원소가 같다면 O(1) +# set() 메서드는 C언어로 처리하기 때문에 실제 leetcode 제출 상 속도는 set을 활용한 경우 # SC: O(n), 최악의 경우 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) class Solution: def containsDuplicate(self, nums: list[int]) -> bool: - unique = set() + numsSet = set(nums) - for num in nums: - if nums in unique: - return True - unique.add(num) + if (len(numsSet) == len(nums)): + return False return True \ No newline at end of file From 4ad0b22db354bc309bc5edc0533d5de21949d355 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Tue, 10 Nov 2026 00:21:06 +0900 Subject: [PATCH 05/15] add missing comments --- contains-duplicate/201411108.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py index 83bf519def..1234952277 100644 --- a/contains-duplicate/201411108.py +++ b/contains-duplicate/201411108.py @@ -1,5 +1,6 @@ # TC: O(n), nums의 원소를 하나씩 확인하기 때문에 O(n), 첫 두 원소가 같다면 O(1) -# set() 메서드는 C언어로 처리하기 때문에 실제 leetcode 제출 상 속도는 set을 활용한 경우 +# set() 메서드는 C언어로 처리하기 때문에 실제 leetcode 제출 상 속도는 set을 활용한 경우 12ms, 첫 원소 확인 코드를 넣을 경우 16ms +# set 메서드를 쓰는 코드가 가독성이 좋다 판단하여 코드 수정 # SC: O(n), 최악의 경우 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) class Solution: def containsDuplicate(self, nums: list[int]) -> bool: From 4685391dabf6cb3356de1ae94e8e7e80a7760362 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Tue, 10 Nov 2026 11:52:19 +0900 Subject: [PATCH 06/15] two sum solution --- two-sum/201411108.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 two-sum/201411108.py diff --git a/two-sum/201411108.py b/two-sum/201411108.py new file mode 100644 index 0000000000..679ae2cd71 --- /dev/null +++ b/two-sum/201411108.py @@ -0,0 +1,11 @@ +from collections import defaultdict + +class Solution: + def twoSum(self, nums: list[int], target: int) -> list[int]: + dictionary = defaultdict(int) + + for i, num in enumerate(nums): + diff = target - num + if diff in dictionary and i != dictionary[diff]: + return [i, dictionary[diff]] + dictionary[num] = i From c0c64c9b295b8c257a04477d1cf5a7ebc802163c Mon Sep 17 00:00:00 2001 From: 201411108 Date: Tue, 10 Nov 2026 12:28:08 +0900 Subject: [PATCH 07/15] add complexity comment --- two-sum/201411108.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/two-sum/201411108.py b/two-sum/201411108.py index 679ae2cd71..b4d2d232ea 100644 --- a/two-sum/201411108.py +++ b/two-sum/201411108.py @@ -1,3 +1,5 @@ +# TC: O(n) +# SC: O(n) from collections import defaultdict class Solution: @@ -9,3 +11,4 @@ def twoSum(self, nums: list[int], target: int) -> list[int]: if diff in dictionary and i != dictionary[diff]: return [i, dictionary[diff]] dictionary[num] = i + From fa037b8fccd42e4d1c7fa1e0de0d0a769326ba01 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Wed, 11 Nov 2026 10:47:18 +0900 Subject: [PATCH 08/15] top k frequent elements solution --- top-k-frequent-elements/201411108.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 top-k-frequent-elements/201411108.py diff --git a/top-k-frequent-elements/201411108.py b/top-k-frequent-elements/201411108.py new file mode 100644 index 0000000000..0fee3d11fc --- /dev/null +++ b/top-k-frequent-elements/201411108.py @@ -0,0 +1,27 @@ +# TC(nlogn), sorted 메서드의 시간 복잡도는 nlogn이다. +# SC(n), k와 nums의 길이가 같을 경우 최악일 것이기 때문에 n일 것이다. +from collections import defaultdict + +key = 0 +value = 1 + +class Solution: + def topKFrequent(self, nums: list[int], k: int) -> list[int]: + dictionary = defaultdict(int) + + # O(n) + for num in nums: + if num in dictionary: + dictionary[num] += 1 + else: + dictionary[num] = 1 + + # O(nlogn) + frequency = sorted(dictionary.items(), key=lambda x: x[value], reverse=True) + result = [] + + for i in range(k): + result.append(frequency[i][key]) + + return result + From 2f58ef33b376f609a9e2699d281bf50bf78a20ed Mon Sep 17 00:00:00 2001 From: 201411108 Date: Wed, 11 Nov 2026 11:19:17 +0900 Subject: [PATCH 09/15] use bucket sort --- top-k-frequent-elements/201411108.py | 30 +++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/top-k-frequent-elements/201411108.py b/top-k-frequent-elements/201411108.py index 0fee3d11fc..0513cb648f 100644 --- a/top-k-frequent-elements/201411108.py +++ b/top-k-frequent-elements/201411108.py @@ -1,27 +1,33 @@ -# TC(nlogn), sorted 메서드의 시간 복잡도는 nlogn이다. -# SC(n), k와 nums의 길이가 같을 경우 최악일 것이기 때문에 n일 것이다. +# n : nums의 길이, m : 고유한 숫자의 개수 +# TC(n), 버킷 정렬에서 n + m 개를 조회하기 때문에 O(n + m) = O(n)이다. (m <= n) +# SC(n) +# - dictionary O(m) +# - buckets O(n + 1 + m), 버킷의 수는 n + 1개, 내부에 저장된 숫자는 m개 +# - result O(k) +# - O(m + n + k)에서 k <= m <= n이므로 O(n) from collections import defaultdict -key = 0 -value = 1 - class Solution: def topKFrequent(self, nums: list[int], k: int) -> list[int]: dictionary = defaultdict(int) - # O(n) for num in nums: if num in dictionary: dictionary[num] += 1 else: dictionary[num] = 1 - # O(nlogn) - frequency = sorted(dictionary.items(), key=lambda x: x[value], reverse=True) + buckets = [[] for _ in range(len(nums) + 1)] + + for num, freq in dictionary.items(): + buckets[freq].append(num) + result = [] - for i in range(k): - result.append(frequency[i][key]) - - return result + # 인덱스가 등장 빈도를 의미하기 때문에 역순으로 조회 + for i in range(len(nums), 0, -1): + for num in buckets[i]: + result.append(num) + if len(result) == k: + return result From 9595fb7e35e2972cb2d7f8f80cea1309ce6278bd Mon Sep 17 00:00:00 2001 From: 201411108 Date: Wed, 11 Nov 2026 11:21:37 +0900 Subject: [PATCH 10/15] fix empty line --- contains-duplicate/201411108.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py index 1234952277..1a54edec18 100644 --- a/contains-duplicate/201411108.py +++ b/contains-duplicate/201411108.py @@ -10,4 +10,5 @@ def containsDuplicate(self, nums: list[int]) -> bool: return False return True - \ No newline at end of file + + \ No newline at end of file From 1008056cf82c3992bab4e224e400ee3d6ebc4c43 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Wed, 11 Nov 2026 11:39:16 +0900 Subject: [PATCH 11/15] delete unused blanks --- contains-duplicate/201411108.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py index 1a54edec18..df3e4b7453 100644 --- a/contains-duplicate/201411108.py +++ b/contains-duplicate/201411108.py @@ -11,4 +11,3 @@ def containsDuplicate(self, nums: list[int]) -> bool: return True - \ No newline at end of file From a25cdb440576f254dcd2a7446ebaab71a6572dd6 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Fri, 13 Nov 2026 15:20:20 +0900 Subject: [PATCH 12/15] longest consecutive sequence solution --- longest-consecutive-sequence/201411108.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-consecutive-sequence/201411108.py diff --git a/longest-consecutive-sequence/201411108.py b/longest-consecutive-sequence/201411108.py new file mode 100644 index 0000000000..686b3d8b23 --- /dev/null +++ b/longest-consecutive-sequence/201411108.py @@ -0,0 +1,22 @@ +# TC: O(n log n), sorted의 시간복잡도, 이후 복잡도는 n +# SC : O(n) +class Solution: + def longestConsecutive(self, nums: list[int]) -> int: + sorted_nums = sorted(nums) + acc = 1 + max_len = 1 + + if len(nums) == 0: + return 0 + + for i, num in enumerate(sorted_nums): + if i != 0: + if num == sorted_nums[i - 1]: + continue + if num - 1 == sorted_nums[i - 1]: + acc += 1 + max_len = max(max_len, acc) + else: + acc = 1 + + return max_len From 41b41a03bfcc2f687a01cc87c8c28a92a1157cdb Mon Sep 17 00:00:00 2001 From: 201411108 Date: Fri, 13 Nov 2026 15:22:02 +0900 Subject: [PATCH 13/15] fix empty line --- longest-consecutive-sequence/201411108.py | 1 + 1 file changed, 1 insertion(+) diff --git a/longest-consecutive-sequence/201411108.py b/longest-consecutive-sequence/201411108.py index 686b3d8b23..5716029f6a 100644 --- a/longest-consecutive-sequence/201411108.py +++ b/longest-consecutive-sequence/201411108.py @@ -20,3 +20,4 @@ def longestConsecutive(self, nums: list[int]) -> int: acc = 1 return max_len + From bb743b059963f224b8fd06a0e6aecfb39d2ddac2 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Fri, 13 Nov 2026 15:46:19 +0900 Subject: [PATCH 14/15] improve time complexity --- longest-consecutive-sequence/201411108.py | 30 ++++++++++------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/longest-consecutive-sequence/201411108.py b/longest-consecutive-sequence/201411108.py index 5716029f6a..9523f3cf0e 100644 --- a/longest-consecutive-sequence/201411108.py +++ b/longest-consecutive-sequence/201411108.py @@ -1,23 +1,19 @@ -# TC: O(n log n), sorted의 시간복잡도, 이후 복잡도는 n -# SC : O(n) +# TC: O(n), set의 원소를 하나씩 확인하기 때문에 O(n) +# SC : O(n), 최대 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) class Solution: def longestConsecutive(self, nums: list[int]) -> int: - sorted_nums = sorted(nums) - acc = 1 - max_len = 1 + nums_set = set(nums) + max_len = 0 - if len(nums) == 0: - return 0 - - for i, num in enumerate(sorted_nums): - if i != 0: - if num == sorted_nums[i - 1]: - continue - if num - 1 == sorted_nums[i - 1]: - acc += 1 - max_len = max(max_len, acc) - else: - acc = 1 + for num in nums_set: + if num - 1 in nums_set: # 이전 숫자가 존재하는(연속 시작점이 있는) 경우에는 continue + continue + + acc = 1 + while num + acc in nums_set: + acc += 1 + + max_len = max(max_len, acc) return max_len From 5fd0e38b9647ea6394ac277513b8ccfdf63691d0 Mon Sep 17 00:00:00 2001 From: 201411108 Date: Fri, 13 Nov 2026 17:49:58 +0900 Subject: [PATCH 15/15] house robber solution --- house-robber/201411108.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 house-robber/201411108.py diff --git a/house-robber/201411108.py b/house-robber/201411108.py new file mode 100644 index 0000000000..af443aac89 --- /dev/null +++ b/house-robber/201411108.py @@ -0,0 +1,20 @@ +# TC: O(n), nums의 원소를 하나씩 확인 +# SC: O(n), 원소 갯수만큼 메모이제이션 배열 +class Solution: + def rob(self, nums: list[int]) -> int: + memo = [None] * len(nums) + + def explore(idx: int) -> int: + if (idx >= len(nums)): + return 0 + + if (memo[idx] is not None): + return memo[idx] + + rob_current = nums[idx] + explore(idx + 2) + skip_current = explore(idx + 1) + memo[idx] = max(rob_current, skip_current) + return memo[idx] + + return explore(0) +