Skip to content

[rivkode] WEEK 01 solutions #1700

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 6 commits into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions contains-duplicate/rivkode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
# The size of the set for storing deduplicated elements is proportional to the length of the input list.

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
list_len = len(nums)
set_len = len(set(nums))
def containsDuplicate(self, nums):
list_num = len(nums)
set_num = len(set(nums))

return list_len != set_len
if list_num != set_num:
return True
else:
return False

if __name__ == "__main__":
solution = Solution()
Expand All @@ -25,3 +28,6 @@ def containsDuplicate(self, nums: list[int]) -> bool:
print(f"start {index} test")
print(f"input : {test}")
print(f"Is valid palindrome ? {solution.containsDuplicate(test)}\n")



21 changes: 21 additions & 0 deletions house-robber/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
memo = {}

def dfs(start):
if start in memo:
return memo[start]
if not start < len(nums):
memo[start] = 0
else:
memo[start] = max(nums[start] + dfs(start + 2), dfs(start + 1))
return memo[start]

return dfs(0)



22 changes: 22 additions & 0 deletions longest-consecutive-sequence/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
num_set = set(nums)
longest = 0

for num in nums:
if num - 1 in num_set:
continue

length = 1
while num + length in num_set:
length += 1
longest = max(length, longest)
return longest




36 changes: 26 additions & 10 deletions top-k-frequent-elements/rivkode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@
# - and when sorting takes O(n), hash[x] occupy O(1)

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
hash = dict()
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""

# for loop nums to set count for each element in hash(dictionary)
for num in nums:
if num in hash:
hash[num] += 1
else:
hash[num] = 1
dic = {}

# sort (TimSort), using lambda function to set a sorting key which is a count
return sorted(hash, key=lambda x: hash[x], reverse=True)[:k]
for v in nums:
dic[v] = dic.get(v, 0) + 1

reverse_desc = sorted(dic.items(), key=lambda item: item[1], reverse=True)

n = 0
result = []
for v in reverse_desc:
if n == k:
break

result.append(v[0])
n += 1

return result

if __name__ == "__main__":
solution = Solution()
Expand All @@ -38,3 +50,7 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
print(f"start{i}")
print(f"input : {nums}, {k}")
print(f"result : {result}")




23 changes: 23 additions & 0 deletions two-sum/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution(object):
def twoSum(self, nums, target):

"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""

dic = {}

for i, v in enumerate(nums):
complement = target - v

if complement in dic:
return [i, dic[complement]]

dic[v] = i