From 36ffedd5dc7d5b1bef0d7a50aa0c4ba9e6b16376 Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:15:45 +0900 Subject: [PATCH 1/7] feat: add 0217 Contains Duplicate solution --- contains-duplicate/hyogshin.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 contains-duplicate/hyogshin.py diff --git a/contains-duplicate/hyogshin.py b/contains-duplicate/hyogshin.py new file mode 100644 index 000000000..f974b0cb6 --- /dev/null +++ b/contains-duplicate/hyogshin.py @@ -0,0 +1,7 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + if len(nums) == len(set(nums)): + return False + else: + return True + \ No newline at end of file From 10ba4e03e7038b66c42638c24aea41b67965b0e3 Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:17:11 +0900 Subject: [PATCH 2/7] feat: add 0219 Two Sum solution --- two-sum/hyogshin.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 two-sum/hyogshin.py diff --git a/two-sum/hyogshin.py b/two-sum/hyogshin.py new file mode 100644 index 000000000..1058402a5 --- /dev/null +++ b/two-sum/hyogshin.py @@ -0,0 +1,14 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + rs = [] + for i in range(len(nums) - 1, -1, -1): + pair = target - nums[i] + if pair not in nums or i == nums.index(pair): + continue + idx = nums.index(pair) + if pair in nums: + rs.append(i) + rs.append(idx) + break + return rs + \ No newline at end of file From 8e79dd91371711ef04336af5ea4710cfde225f54 Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:18:39 +0900 Subject: [PATCH 3/7] feat: add 0237 Top K Frequent Elements solution --- top-k-frequent-elements/hyogshin.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 top-k-frequent-elements/hyogshin.py diff --git a/top-k-frequent-elements/hyogshin.py b/top-k-frequent-elements/hyogshin.py new file mode 100644 index 000000000..00102b814 --- /dev/null +++ b/top-k-frequent-elements/hyogshin.py @@ -0,0 +1,24 @@ +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + plus = [0] * (10**4 + 1) + minus = [0] * (10**4 + 1) + + for i in range(len(nums)): + if nums[i] < 0: + minus[-(nums[i])] += 1 + else: + plus[nums[i]] += 1 + + ans = [] + for i in range(k): + if max(max(minus), max(plus)) == max(plus): + idx = plus.index(max(plus)) + ans.append(idx) + plus[idx] = 0 + else: + idx = minus.index(max(minus)) + ans.append(-(idx)) + minus[idx] = 0 + + return ans + \ No newline at end of file From 4ff4946c08b97006d7e02570d57c5a6d1306378a Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:19:47 +0900 Subject: [PATCH 4/7] feat: add 0240 Longest Consecutive Sequence solution --- longest-consecutive-sequence/hyogshin.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 longest-consecutive-sequence/hyogshin.py diff --git a/longest-consecutive-sequence/hyogshin.py b/longest-consecutive-sequence/hyogshin.py new file mode 100644 index 000000000..9fdb93de6 --- /dev/null +++ b/longest-consecutive-sequence/hyogshin.py @@ -0,0 +1,19 @@ +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if not nums: + return 0 + s = set(nums) + nums = sorted(list(s)) + rs = [] + cnt = 1 + + for i in range(len(nums)-1): + if (nums[i] + 1) == nums[i+1]: + cnt += 1 + else: + rs.append(cnt) + cnt = 1 + + rs.append(cnt) + return max(rs) + \ No newline at end of file From 80ad178766f963444b7b818255625406466d8579 Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:20:34 +0900 Subject: [PATCH 5/7] feat: add 0264 House Robber solution --- house-robber/hyogshin.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 house-robber/hyogshin.py diff --git a/house-robber/hyogshin.py b/house-robber/hyogshin.py new file mode 100644 index 000000000..9d12db4a6 --- /dev/null +++ b/house-robber/hyogshin.py @@ -0,0 +1,12 @@ +class Solution: + def rob(self, nums: List[int]) -> int: + dp = [0] * (len(nums)) + dp[0] = nums[0] + if len(nums) > 1: + dp[1] = max(dp[0], nums[1]) + + for i in range(2, len(nums)): + dp[i] = max(dp[i-2] + nums[i], dp[i-1]) + + return max(dp) + \ No newline at end of file From 54b9019b4fa0a1432b7f9de1206113c2603d3ad4 Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:34:23 +0900 Subject: [PATCH 6/7] docs: add time and space complexity --- contains-duplicate/hyogshin.py | 8 +++++++- house-robber/hyogshin.py | 6 +++++- longest-consecutive-sequence/hyogshin.py | 6 +++++- top-k-frequent-elements/hyogshin.py | 6 +++++- two-sum/hyogshin.py | 6 +++++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/hyogshin.py b/contains-duplicate/hyogshin.py index f974b0cb6..8ae9bf903 100644 --- a/contains-duplicate/hyogshin.py +++ b/contains-duplicate/hyogshin.py @@ -4,4 +4,10 @@ def containsDuplicate(self, nums: List[int]) -> bool: return False else: return True - \ No newline at end of file + + + +''' +시간 복잡도: O(n)?? +공간 복잡도: set을 활용해 nums 리스트를 복사함 -> len(nums)의 2배? +''' diff --git a/house-robber/hyogshin.py b/house-robber/hyogshin.py index 9d12db4a6..530451508 100644 --- a/house-robber/hyogshin.py +++ b/house-robber/hyogshin.py @@ -9,4 +9,8 @@ def rob(self, nums: List[int]) -> int: dp[i] = max(dp[i-2] + nums[i], dp[i-1]) return max(dp) - \ No newline at end of file + +''' +시간 복잡도: for loop -> O(n) +공간 복잡도: dp 배열 +''' diff --git a/longest-consecutive-sequence/hyogshin.py b/longest-consecutive-sequence/hyogshin.py index 9fdb93de6..95c55fc4c 100644 --- a/longest-consecutive-sequence/hyogshin.py +++ b/longest-consecutive-sequence/hyogshin.py @@ -16,4 +16,8 @@ def longestConsecutive(self, nums: List[int]) -> int: rs.append(cnt) return max(rs) - \ No newline at end of file + +''' +시간 복잡도: for loop 1회 -> O(n) +공간 복잡도: nums 리스트 + rs 배열 (최대 len(nums)) + sorted로 nums 배열 복사 +''' diff --git a/top-k-frequent-elements/hyogshin.py b/top-k-frequent-elements/hyogshin.py index 00102b814..8e21c19fd 100644 --- a/top-k-frequent-elements/hyogshin.py +++ b/top-k-frequent-elements/hyogshin.py @@ -21,4 +21,8 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: minus[idx] = 0 return ans - \ No newline at end of file + +''' +시간 복잡도: for loop 2회 -> O(2n) -> O(n) +공간 복잡도: 배열 크기 (10**4 + 1) 두 개 + nums 리스트 + ans 배열 +''' diff --git a/two-sum/hyogshin.py b/two-sum/hyogshin.py index 1058402a5..f56c05e89 100644 --- a/two-sum/hyogshin.py +++ b/two-sum/hyogshin.py @@ -11,4 +11,8 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: rs.append(idx) break return rs - \ No newline at end of file + +''' +시간 복잡도: for loop 사용 -> O(n) +공간 복잡도: len(nums) + rs 배열에서 number 2개 저장 +''' From e5950a90f63413fd67a98aeed491e8e876c0d35b Mon Sep 17 00:00:00 2001 From: Hyogyeong Shin Date: Sat, 26 Jul 2025 12:51:40 +0900 Subject: [PATCH 7/7] docs: fix complexity comments --- contains-duplicate/hyogshin.py | 8 ++++++-- house-robber/hyogshin.py | 13 +++++++++++-- longest-consecutive-sequence/hyogshin.py | 13 +++++++++++-- top-k-frequent-elements/hyogshin.py | 9 +++++++-- two-sum/hyogshin.py | 9 +++++++-- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/contains-duplicate/hyogshin.py b/contains-duplicate/hyogshin.py index 8ae9bf903..84f95c69b 100644 --- a/contains-duplicate/hyogshin.py +++ b/contains-duplicate/hyogshin.py @@ -8,6 +8,10 @@ def containsDuplicate(self, nums: List[int]) -> bool: ''' -시간 복잡도: O(n)?? -공간 복잡도: set을 활용해 nums 리스트를 복사함 -> len(nums)의 2배? +시간 복잡도: O(n) +- set(nums)는 내부적으로 nums의 모든 원소에 대해 __hash__() 및 __eq__() 호출 -> O(n) +- len() 함수는 O(1) -> 무시 + +공간 복잡도: O(n) +- set(nums)는 nums의 원소를 모두 저장할 수 있게 공간 사용 -> 최악의 경우 O(n) ''' diff --git a/house-robber/hyogshin.py b/house-robber/hyogshin.py index 530451508..c1be83a34 100644 --- a/house-robber/hyogshin.py +++ b/house-robber/hyogshin.py @@ -11,6 +11,15 @@ def rob(self, nums: List[int]) -> int: return max(dp) ''' -시간 복잡도: for loop -> O(n) -공간 복잡도: dp 배열 +시간 복잡도: O(n log n) +- set() -> O(n) +- sorted() -> O(n log n) +- for loop -> O(n) +- max() -> O(n) + +공간 복잡도: O(n) +- set() -> O(n) +- sorted list -> O(n) +- rs -> O(n) +- O(3n) => O(n) ''' diff --git a/longest-consecutive-sequence/hyogshin.py b/longest-consecutive-sequence/hyogshin.py index 95c55fc4c..87db1bcab 100644 --- a/longest-consecutive-sequence/hyogshin.py +++ b/longest-consecutive-sequence/hyogshin.py @@ -18,6 +18,15 @@ def longestConsecutive(self, nums: List[int]) -> int: return max(rs) ''' -시간 복잡도: for loop 1회 -> O(n) -공간 복잡도: nums 리스트 + rs 배열 (최대 len(nums)) + sorted로 nums 배열 복사 +시간 복잡도: O(n log n) +- set(nums) -> O(n) +- sorted(list(s)) -> O(n log n) +- for loop -> O(n) +- O(2n) + O(n log n) => O(2n) 이 아니라 왜 O(n log n) 이지? + +공간 복잡도: O(n) +- set -> O(n) +- sorted() -> O(n) +- rs -> O(n) +- O(3n) => O(n) ''' diff --git a/top-k-frequent-elements/hyogshin.py b/top-k-frequent-elements/hyogshin.py index 8e21c19fd..e4b75722a 100644 --- a/top-k-frequent-elements/hyogshin.py +++ b/top-k-frequent-elements/hyogshin.py @@ -23,6 +23,11 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: return ans ''' -시간 복잡도: for loop 2회 -> O(2n) -> O(n) -공간 복잡도: 배열 크기 (10**4 + 1) 두 개 + nums 리스트 + ans 배열 +시간 복잡도: O(1) +- for loop -> 보통 O(n) 이지만, 길이 10001 짜리 고정 배열 -> O(1)로 취급 가능 + +공간 복잡도: 입력 제외 -> O(1), 입력 포함 -> O(n) +- plus 배열: 10001 -> O(1) +- minus 배열: 10001 -> O(1) +- ans 배열: 길이 k -> O(k) ''' diff --git a/two-sum/hyogshin.py b/two-sum/hyogshin.py index f56c05e89..4b9a6ff2a 100644 --- a/two-sum/hyogshin.py +++ b/two-sum/hyogshin.py @@ -13,6 +13,11 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: return rs ''' -시간 복잡도: for loop 사용 -> O(n) -공간 복잡도: len(nums) + rs 배열에서 number 2개 저장 +시간 복잡도: O(n^2) +- nums.index(pair) -> O(n) +- for loop 안에서 nums.index(pair) 최대 2번 호출 -> O(2n^2) -> O(n^2) + +공간 복잡도: O(1) +- rs 배열에서 number 2개 저장 -> O(1) 공간 +- nums 복사나 set/dict 없음 '''