From 5821b74fdea0938a5f523ee82b0ce76489b41e1f Mon Sep 17 00:00:00 2001 From: yejin Date: Mon, 17 Nov 2025 16:14:25 +0900 Subject: [PATCH 1/3] contains-duplicate solution --- contains-duplicate/devyejin.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/contains-duplicate/devyejin.py b/contains-duplicate/devyejin.py index 32b66eabb8..d5e15b82ae 100644 --- a/contains-duplicate/devyejin.py +++ b/contains-duplicate/devyejin.py @@ -1,4 +1,9 @@ -class Solution(object): - def containsDuplicate(self, nums): - return len(nums) != len(set(nums)) +from typing import List +""" +time complexity : O(n) +space complexity : O(n) +""" +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(set(nums)) == len(nums) From 85c431ddaeac4fb8d7db428d114640313a2d5481 Mon Sep 17 00:00:00 2001 From: yejin Date: Mon, 17 Nov 2025 18:54:57 +0900 Subject: [PATCH 2/3] two-sum solution --- two-sum/devyejin.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/two-sum/devyejin.py b/two-sum/devyejin.py index be0dc862d0..568a98436e 100644 --- a/two-sum/devyejin.py +++ b/two-sum/devyejin.py @@ -1,15 +1,12 @@ -class Solution(object): - def twoSum(self, nums, target): +from typing import List - nums_tuple = sorted(list(enumerate(nums)), key=lambda x: x[1]) - left, right = 0, len(nums) - 1 - while left < right: - temp_sum = nums_tuple[left][1] + nums_tuple[right][1] - if temp_sum == target: - return [nums_tuple[left][0], nums_tuple[right][0]] - elif temp_sum < target: - left += 1 - else: - right -= 1 +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + seen = {} + for idx, num in enumerate(nums): + need = target - num + if need in seen: + return [idx, seen[need]] + seen[num] = idx From fb339702d3ce8f47597237dbca8d55ede4bef546 Mon Sep 17 00:00:00 2001 From: yejin Date: Mon, 17 Nov 2025 23:53:39 +0900 Subject: [PATCH 3/3] top-k-frequent-elements solution --- top-k-frequent-elements/devyejin.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/top-k-frequent-elements/devyejin.py b/top-k-frequent-elements/devyejin.py index 8a082da5b7..6c11d0c308 100644 --- a/top-k-frequent-elements/devyejin.py +++ b/top-k-frequent-elements/devyejin.py @@ -1,9 +1,16 @@ +""" +time complexity: O(nlogn) +space complexity: O(n) +""" + +from typing import List from collections import Counter -import heapq +from heapq import nlargest + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + count = Counter(nums) + return [num for num, _ in nlargest(k, count.items(), key=lambda x: x[1])] -class Solution(object): - def topKFrequent(self, nums, k): - counter = sorted(Counter(nums).items(), key=lambda item: -item[1]) - return list(num for num, count in counter[:k])