diff --git a/contains-duplicate/daehyun99.py b/contains-duplicate/daehyun99.py new file mode 100644 index 0000000000..44b3d7c2e9 --- /dev/null +++ b/contains-duplicate/daehyun99.py @@ -0,0 +1,10 @@ +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 diff --git a/top-k-frequent-elements/daehyun99.py b/top-k-frequent-elements/daehyun99.py new file mode 100644 index 0000000000..5aca3e1a1f --- /dev/null +++ b/top-k-frequent-elements/daehyun99.py @@ -0,0 +1,22 @@ +# Time: O(nk) +# Space: O(n) +from collections import defaultdict + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + nums_dict = defaultdict(int) + + for num in nums: + nums_dict[num] += 1 + + result = [] + for i in range(k): + max_count = 0 + max_num = 0 + for num, count in nums_dict.items(): + if max_count < count: + max_count = count + max_num = num + nums_dict[max_num] = 0 + result.append(max_num) + return result diff --git a/two-sum/daehyun99.py b/two-sum/daehyun99.py new file mode 100644 index 0000000000..8385da2f7f --- /dev/null +++ b/two-sum/daehyun99.py @@ -0,0 +1,24 @@ +# Time: O(n) +# Space: O(2n) +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + set_nums = set(nums) + for j in range(len(nums)-1, -1, -1): + val = target - nums[j] + if val in set_nums: + i = nums.index(val) + if i != j: + return [i, j] + +""" +# Time: O(n^2) +# Space: O(1) +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)-1): + for j in range(i+1, len(nums)): + if nums[i] + nums[j] == target: + return [i, j] + + +"""