Skip to content

Commit b24402a

Browse files
authored
Merge pull request #2040 from devyejin/main
[devyejin] WEEK 01 solutions
2 parents 84e1fe9 + fb33970 commit b24402a

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

contains-duplicate/devyejin.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
class Solution(object):
2-
def containsDuplicate(self, nums):
3-
return len(nums) != len(set(nums))
1+
from typing import List
2+
"""
3+
time complexity : O(n)
4+
space complexity : O(n)
5+
"""
6+
class Solution:
7+
def containsDuplicate(self, nums: List[int]) -> bool:
8+
return len(set(nums)) == len(nums)
49

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
"""
2+
time complexity: O(nlogn)
3+
space complexity: O(n)
4+
"""
5+
6+
from typing import List
17
from collections import Counter
2-
import heapq
8+
from heapq import nlargest
9+
10+
class Solution:
11+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
12+
count = Counter(nums)
13+
return [num for num, _ in nlargest(k, count.items(), key=lambda x: x[1])]
314

415

5-
class Solution(object):
6-
def topKFrequent(self, nums, k):
7-
counter = sorted(Counter(nums).items(), key=lambda item: -item[1])
8-
return list(num for num, count in counter[:k])
916

two-sum/devyejin.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
class Solution(object):
2-
def twoSum(self, nums, target):
1+
from typing import List
32

4-
nums_tuple = sorted(list(enumerate(nums)), key=lambda x: x[1])
5-
left, right = 0, len(nums) - 1
63

7-
while left < right:
8-
temp_sum = nums_tuple[left][1] + nums_tuple[right][1]
9-
if temp_sum == target:
10-
return [nums_tuple[left][0], nums_tuple[right][0]]
11-
elif temp_sum < target:
12-
left += 1
13-
else:
14-
right -= 1
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
seen = {}
7+
for idx, num in enumerate(nums):
8+
need = target - num
9+
if need in seen:
10+
return [idx, seen[need]]
11+
seen[num] = idx
1512

0 commit comments

Comments
 (0)