Skip to content

Commit a7426ad

Browse files
authored
Merge pull request #1978 from WHYjun/main
[WHYjun] WEEK 01 solutions
2 parents 0fc7ec8 + 1f31fbe commit a7426ad

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

contains-duplicate/WHYjun.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
return len(set(nums)) != len(nums)

house-robber/WHYjun.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
# memo for DP
4+
dp = {}
5+
return self.robs(nums, dp, len(nums) - 1)
6+
7+
def robs(self, nums: List[int], dp: Dict[int, int], houseToRob: int) -> int:
8+
if houseToRob < 0:
9+
return 0
10+
11+
if houseToRob - 2 not in dp:
12+
dp[houseToRob - 2] = self.robs(nums, dp, houseToRob - 2)
13+
14+
if houseToRob - 1 not in dp:
15+
dp[houseToRob - 1] = self.robs(nums, dp, houseToRob - 1)
16+
17+
18+
return max(
19+
dp[houseToRob - 2] + nums[houseToRob],
20+
dp[houseToRob - 1]
21+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import heapq
2+
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
# remove duplicates by using set
6+
numsSet = set(nums)
7+
8+
if len(numsSet) == 0 or len(numsSet) == 1:
9+
return len(numsSet)
10+
11+
# Use priority queue to sort in O(n)
12+
pq = heapq.heapify(list(numsSet))
13+
14+
prev = heapq.heappop(pq)
15+
answer, count = 1, 1
16+
for i in range(len(numsSet) - 1):
17+
popped = heapq.heappop(pq)
18+
if prev + 1 == popped:
19+
count += 1
20+
else:
21+
count = 1
22+
23+
prev = popped
24+
answer = max(answer, count)
25+
26+
return answer
27+
28+
def longestConsecutiveUsingSetOnly(self, nums: List[int]) -> int:
29+
# remove duplicates by using set
30+
numsSet = set(nums)
31+
32+
answer = 0
33+
34+
for num in numsSet:
35+
# continue if it's not the longest consecutive
36+
if num - 1 in numsSet:
37+
continue
38+
else:
39+
count = 1
40+
while num + count in numsSet:
41+
count += 1
42+
answer = max(answer, count)
43+
44+
return answer

top-k-frequent-elements/WHYjun.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
# Time complexity: O(nlog(n))
4+
# Space Complexity: O(n)
5+
6+
if len(nums) <= k:
7+
return list(set(nums))
8+
9+
counts = {}
10+
for num in nums:
11+
if num not in counts:
12+
counts[num] = 0
13+
counts[num] += 1
14+
15+
# The key of dictionary is unique.
16+
sortedKeys = sorted(list(counts.keys()), key=lambda key: counts[key], reverse = True)
17+
return sortedKeys[:k]

two-sum/WHYjun.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
memo = {}
4+
5+
for i, num in enumerate(nums):
6+
diff = target - num
7+
if diff in memo:
8+
# Each input would have exactly one solution
9+
return [memo[diff], i]
10+
memo[num] = i
11+
12+
return []

0 commit comments

Comments
 (0)