Skip to content
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

[hodaessi] Week 1 #692

Closed
wants to merge 5 commits into from
Closed
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
11 changes: 11 additions & 0 deletions contains-duplicate/hodaessi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import List

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
dict = {}
for num in nums:
dict[num] = dict.get(num, 0) + 1
if dict[num] > 1:
return True
return False

15 changes: 15 additions & 0 deletions house-robber/hodaessi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List


class Solution:
def rob(self, nums: List[int]) -> int:
dp = [0] * len(nums)

for i in range(len(nums)-1, -1, -1):
dpMax = 0
for j in range(i + 2, len(nums)):
dpMax = max(dpMax, dp[j])
dp[i] = nums[i] + dpMax

return max(dp)

38 changes: 38 additions & 0 deletions longest-consecutive-sequence/hodaessi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import List


class Node:
def __init__(self, value):
self.value = value
self.parent = None
self.child = None

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
answer = 0
dict = {}

for num in nums:
if dict.get(num) is None:
dict[num] = Node(num)
if dict.get(num + 1) is not None:
dict[num + 1].child = dict[num]
dict[num].parent = dict[num + 1]

if dict.get(num - 1) is not None:
dict[num].child = dict[num - 1]
dict[num - 1].parent = dict[num]

for key in dict.keys():
if dict[key].parent is None:
node = dict[key]
count = 1

while node.child is not None:
count += 1
node = node.child

answer = max(answer, count)

return answer

10 changes: 10 additions & 0 deletions top-k-frequent-elements/hodaessi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import List

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
dict = {}
for num in nums:
dict[num] = dict.get(num, 0) + 1

return sorted(dict.keys(), key=lambda x: dict[x], reverse=True)[:k]

8 changes: 8 additions & 0 deletions valid-palindrome/hodaessi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.lower()

s = ''.join(filter(str.isalnum, s))

return s == s[::-1]

Loading