diff --git a/contains-duplicate/201411108.py b/contains-duplicate/201411108.py new file mode 100644 index 0000000000..df3e4b7453 --- /dev/null +++ b/contains-duplicate/201411108.py @@ -0,0 +1,13 @@ +# TC: O(n), nums의 원소를 하나씩 확인하기 때문에 O(n), 첫 두 원소가 같다면 O(1) +# set() 메서드는 C언어로 처리하기 때문에 실제 leetcode 제출 상 속도는 set을 활용한 경우 12ms, 첫 원소 확인 코드를 넣을 경우 16ms +# set 메서드를 쓰는 코드가 가독성이 좋다 판단하여 코드 수정 +# SC: O(n), 최악의 경우 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) +class Solution: + def containsDuplicate(self, nums: list[int]) -> bool: + numsSet = set(nums) + + if (len(numsSet) == len(nums)): + return False + + return True + 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) + diff --git a/longest-consecutive-sequence/201411108.py b/longest-consecutive-sequence/201411108.py new file mode 100644 index 0000000000..9523f3cf0e --- /dev/null +++ b/longest-consecutive-sequence/201411108.py @@ -0,0 +1,19 @@ +# TC: O(n), set의 원소를 하나씩 확인하기 때문에 O(n) +# SC : O(n), 최대 n개의 원소가 set에 들어갈 수 있기 때문에 O(n) +class Solution: + def longestConsecutive(self, nums: list[int]) -> int: + nums_set = set(nums) + max_len = 0 + + 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 + diff --git a/top-k-frequent-elements/201411108.py b/top-k-frequent-elements/201411108.py new file mode 100644 index 0000000000..0513cb648f --- /dev/null +++ b/top-k-frequent-elements/201411108.py @@ -0,0 +1,33 @@ +# 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 + +class Solution: + def topKFrequent(self, nums: list[int], k: int) -> list[int]: + dictionary = defaultdict(int) + + for num in nums: + if num in dictionary: + dictionary[num] += 1 + else: + dictionary[num] = 1 + + buckets = [[] for _ in range(len(nums) + 1)] + + for num, freq in dictionary.items(): + buckets[freq].append(num) + + result = [] + + # 인덱스가 등장 빈도를 의미하기 때문에 역순으로 조회 + for i in range(len(nums), 0, -1): + for num in buckets[i]: + result.append(num) + if len(result) == k: + return result + diff --git a/two-sum/201411108.py b/two-sum/201411108.py new file mode 100644 index 0000000000..b4d2d232ea --- /dev/null +++ b/two-sum/201411108.py @@ -0,0 +1,14 @@ +# TC: O(n) +# SC: O(n) +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 +