Skip to content

Commit ded985a

Browse files
Merge pull request #1183 from printjin-gmailcom/main
[printjin-gmailcom] WEEK 01 solutions
2 parents d790347 + 8c95eed commit ded985a

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed
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):
3+
return len(nums) != len(set(nums))

house-robber/printjin-gmailcom.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def rob(self, nums):
3+
num_map = {}
4+
for i, num in enumerate(nums):
5+
left = num_map.get(i - 2, 0)
6+
leftleft = num_map.get(i - 3, 0)
7+
num_map[i] = max(left + num, leftleft + num)
8+
return max(num_map.get(len(nums) - 1, 0), num_map.get(len(nums) - 2, 0))
9+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def longestConsecutive(self, nums):
3+
num_set = set(nums)
4+
longest = 0
5+
for num in num_set:
6+
if num - 1 not in num_set:
7+
current = num
8+
length = 1
9+
while current + 1 in num_set:
10+
current += 1
11+
length += 1
12+
longest = max(longest, length)
13+
return longest
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def topKFrequent(self, nums, k):
5+
count = Counter(nums)
6+
return [num for num, _ in count.most_common(k)]

two-sum/printjin-gmailcom.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def twoSum(self, nums, target):
3+
num_map = {}
4+
for i, num in enumerate(nums):
5+
left = target - num
6+
if left in num_map:
7+
return [num_map[left], i]
8+
num_map[num] = i
9+

0 commit comments

Comments
 (0)