diff --git a/contains-duplicate/freemjstudio.py b/contains-duplicate/freemjstudio.py new file mode 100644 index 0000000000..0273cbd4e3 --- /dev/null +++ b/contains-duplicate/freemjstudio.py @@ -0,0 +1,8 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + first = len(nums) # O(1) : 리스트 객체는 이미 자기자신의 길이를 저장하고 있음 + set_nums = set(nums) # O(N) + second = len(set_nums) + return (first != second) + +# 시간 복잡도 : O(N) \ No newline at end of file diff --git a/top-k-frequent-elements/freemjstudio.py b/top-k-frequent-elements/freemjstudio.py new file mode 100644 index 0000000000..184c87aeee --- /dev/null +++ b/top-k-frequent-elements/freemjstudio.py @@ -0,0 +1,29 @@ +import heapq + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + answer = [] + count = dict() + heap = [] + if len(nums) <= 1: + return nums + # O(N) + for num in nums: + count[num] = count.get(num, 0) + 1 + + # heap 의 크기는 최대 k 로 제한함. + # unique 한 M개의 숫자를 선형 순회 O(M) + for num, freq in count.items(): + if len(heap) < k: + heapq.heappush(heap, (freq, num)) # O(logK) + else: + if heap[0][0] < freq: + heapq.heappop(heap) # O(logK) + heapq.heappush(heap, (freq, num)) # O(logK) + + for _ in range(k): + k, v = heapq.heappop(heap) + answer.append(v) + + return answer + diff --git a/two-sum/freemjstudio.py b/two-sum/freemjstudio.py new file mode 100644 index 0000000000..c36de0f08d --- /dev/null +++ b/two-sum/freemjstudio.py @@ -0,0 +1,13 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + answer = [] + hashmap = dict() + for i in range(len(nums)): + current_num = nums[i] + diff = target - current_num + if diff in hashmap.keys(): + return [i, hashmap[diff]] + hashmap[current_num] = i # store the index of current num + + return answer + \ No newline at end of file