Skip to content

[printjin-gmailcom] WEEK 01 solutions #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 5, 2025
3 changes: 3 additions & 0 deletions contains-duplicate/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def containsDuplicate(self, nums):
return len(nums) != len(set(nums))
9 changes: 9 additions & 0 deletions house-robber/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def rob(self, nums):
num_map = {}
for i, num in enumerate(nums):
left = num_map.get(i - 2, 0)
leftleft = num_map.get(i - 3, 0)
num_map[i] = max(left + num, leftleft + num)
return max(num_map.get(len(nums) - 1, 0), num_map.get(len(nums) - 2, 0))

13 changes: 13 additions & 0 deletions longest-consecutive-sequence/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def longestConsecutive(self, nums):
num_set = set(nums)
longest = 0
for num in num_set:
if num - 1 not in num_set:
current = num
length = 1
while current + 1 in num_set:
current += 1
length += 1
longest = max(longest, length)
return longest
6 changes: 6 additions & 0 deletions top-k-frequent-elements/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from collections import Counter

class Solution:
def topKFrequent(self, nums, k):
count = Counter(nums)
return [num for num, _ in count.most_common(k)]
9 changes: 9 additions & 0 deletions two-sum/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def twoSum(self, nums, target):
num_map = {}
for i, num in enumerate(nums):
left = target - num
if left in num_map:
return [num_map[left], i]
num_map[num] = i