From cca18723c3a66e53a49061cf49735a2aa8a3cbbd Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Mon, 21 Jul 2025 07:37:35 +0900 Subject: [PATCH 1/5] contains-duplicate sol (py) --- contains-duplicate/hi-rachel.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 contains-duplicate/hi-rachel.py diff --git a/contains-duplicate/hi-rachel.py b/contains-duplicate/hi-rachel.py new file mode 100644 index 000000000..a70d566f6 --- /dev/null +++ b/contains-duplicate/hi-rachel.py @@ -0,0 +1,28 @@ +""" +https://leetcode.com/problems/contains-duplicate/ + +Given an integer array nums, +return true if any value appears at least twice in the array, +and return false if every element is distinct. + +TC: O(n) +SC: O(n) +""" + +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + seen = set() + + for num in nums: + if num in seen: + return True + else: + seen.add(num) + return False + + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(set(nums)) != len(nums) From e4c8c55372132931fb818781571b046c5de9c924 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Mon, 21 Jul 2025 07:59:10 +0900 Subject: [PATCH 2/5] two-sum sol (py) --- two-sum/hi-rachel.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/two-sum/hi-rachel.py b/two-sum/hi-rachel.py index 4b317cdc0..3a0bdd17e 100644 --- a/two-sum/hi-rachel.py +++ b/two-sum/hi-rachel.py @@ -1,25 +1,17 @@ """ -처음 풀이 -O(N^2) time, O(N) space -""" +https://leetcode.com/problems/two-sum/ -# class Solution: -# def twoSum(self, nums: List[int], target: int) -> List[int]: -# result = [] -# for i in range(len(nums)): -# rest = target - nums[i] -# rest_nums = nums[i+1:] -# if rest in rest_nums: -# result.extend([i, rest_nums.index(rest)+i+1]) -# break -# return result - +Given an array of integers nums and an integer target, +return indices of the two numbers such that they add up to target. +You may assume that each input would have exactly one solution, and you may not use the same element twice. +You can return the answer in any order. -""" 개선 코드 O(N) time, O(N) space """ +from typing import List + class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: indices = {} @@ -31,6 +23,24 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: return [i, j] indices[v] = i + +""" +처음 풀이 +O(N^2) time, O(N) space +""" + +# class Solution: +# def twoSum(self, nums: List[int], target: int) -> List[int]: +# result = [] +# for i in range(len(nums)): +# rest = target - nums[i] +# rest_nums = nums[i+1:] +# if rest in rest_nums: +# result.extend([i, rest_nums.index(rest)+i+1]) +# break +# return result + + # JS 풀이 # /** # * @param {number[]} nums From dd9d0248b68600283d0cafee14d43e4ffaa8d875 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Mon, 21 Jul 2025 12:36:09 +0900 Subject: [PATCH 3/5] top-k-frequent-elements sol (py) --- top-k-frequent-elements/hi-rachel.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/top-k-frequent-elements/hi-rachel.py b/top-k-frequent-elements/hi-rachel.py index 8d0aac0ca..d36ddfce8 100644 --- a/top-k-frequent-elements/hi-rachel.py +++ b/top-k-frequent-elements/hi-rachel.py @@ -1,11 +1,19 @@ -# 가장 자주 등장한 상위 K개의 문자 배열 반환 -# O(n log n) time, O(n) space +""" +https://leetcode.com/problems/top-k-frequent-elements/ + +Given an integer array nums and an integer k, return the k most frequent elements. +You may return the answer in any order. + +TC: O(n log n) +SC: O(n) +""" from collections import defaultdict +from typing import List class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: - numdict = defaultdict(int); + numdict = defaultdict(int) for num in nums: numdict[num] += 1 @@ -15,3 +23,14 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: keys = list(sort_dict) return keys[:k] + +# heapq 풀이 +from collections import Counter +import heapq + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + counter = Counter(nums) + return heapq.nlargest(k, counter.keys(), key=counter.get) + + From 40c0d008f848d4738c0248e97bb373e9ebc43594 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Wed, 23 Jul 2025 18:50:32 +0900 Subject: [PATCH 4/5] longest-consecutive-sequence sol (py) --- longest-consecutive-sequence/hi-rachel.py | 42 ++++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/longest-consecutive-sequence/hi-rachel.py b/longest-consecutive-sequence/hi-rachel.py index bc6a3265a..adcdd2716 100644 --- a/longest-consecutive-sequence/hi-rachel.py +++ b/longest-consecutive-sequence/hi-rachel.py @@ -1,6 +1,8 @@ # 처음 풀이 # O(n log n) time, O(n) space +from typing import List + class Solution: def longestConsecutive(self, nums: List[int]) -> int: if nums == []: @@ -67,11 +69,19 @@ def longestConsecutive(self, nums: List[int]) -> int: return longest -# 최종 개선 풀이 -# O(n) time, O(n) space -# 위 풀이에서 한쪽으로 구간을 찾지 않고, 양쪽으로 찾으며 숫자를 집합에서 제거하며 -# 집합에서 원소가 하나도 남지 않을 때까지 하면 가장 긴 구간의 길이를 구할 수 있다. -# 배열의 모든 정수를 set에 저장했으므로 공간 복잡도는 O(n) +""" +최종 개선 풀이 +O(n) time, O(n) space + +위 풀이에서 한쪽으로 구간을 찾지 않고, 양쪽으로 찾으며 숫자를 집합에서 제거하며 +집합에서 원소가 하나도 남지 않을 때까지 하면 가장 긴 구간의 길이를 구할 수 있다. + +배열의 모든 정수를 set에 저장했으므로 공간 복잡도는 O(n), 시간 복잡도 O(n) +set 삽입: O(1), 삭제: 평균 O(1), 순서 보장 x => 순서를 무시하고 존재 여부(빠른 탐색)이 중요할 때 사용. +존재하지 않는 값을 pop(), remove()시 KeyError 주의 + +max(a, b): 두 값 중 큰 값을 반환 → O(1) +""" class Solution: def longestConsecutive(self, nums: List[int]) -> int: @@ -93,6 +103,28 @@ def longestConsecutive(self, nums: List[int]) -> int: return longest +""" +25/7/23 복습, 위 최적화 풀이로 풀지 못함 +TC: O(n log n) +SC: O(n) +""" +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if not nums: + return 0 + nums = sorted(list(set(nums))) + max_cnt = 1 + cnt = 1 + + for i in range(len(nums) - 1): + if nums[i] + 1 == nums[i + 1]: + cnt += 1 + max_cnt = max(max_cnt, cnt) + else: + cnt = 1 + + return max_cnt + # TS 풀이 # O(n) time, O(n) space # JavaScript Set에서 값을 꺼내고자 할때는 **numSet.values().next().value** 사용 From 8972f8d0e7277ee408a9ab8ea9885246d4a98417 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Wed, 23 Jul 2025 21:10:10 +0900 Subject: [PATCH 5/5] house-robber sol (py) --- house-robber/hi-rachel.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/house-robber/hi-rachel.py b/house-robber/hi-rachel.py index 3b99750bc..cd0687e64 100644 --- a/house-robber/hi-rachel.py +++ b/house-robber/hi-rachel.py @@ -10,20 +10,38 @@ # **dp[i] = max(dp[i-1], dp[i-2] + nums[i])** # nums 길이가 2인 경우 range(2, 2)는 for문 안 돈다. +from typing import List + class Solution: def rob(self, nums: List[int]) -> int: - if not nums: return 0 - if len(nums) == 1: return nums[0] - + if len(nums) == 1: + return nums[0] + dp = [0] * len(nums) dp[0] = nums[0] dp[1] = max(nums[0], nums[1]) for i in range(2, len(nums)): - dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]) + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) return dp[-1] +""" +공간 최적화 풀이 + +prev2: i-2까지의 최대값 + 현재 돈 +prev1: i번째 집 안 털고, 이전까지의 최대값 유지 + +TC: O(n) +SC: O(1) +""" +class Solution: + def rob(self, nums: List[int]) -> int: + prev2, prev1 = 0, 0 + for num in nums: + prev2, prev1 = prev1, max(prev1, prev2 + num) + return prev1 + # TS 코드 # function rob(nums: number[]): number {