Skip to content

Commit

Permalink
Merge pull request #646 from Jeldo/main
Browse files Browse the repository at this point in the history
[jeldo3] Week1
  • Loading branch information
Jeldo authored Dec 8, 2024
2 parents 8901b1d + 7baf829 commit b8b7c86
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contains-duplicate/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
# O(n)
def containsDuplicate(self, nums: list[int]) -> bool:
return len(nums) != len(set(nums)) # O(n)
9 changes: 9 additions & 0 deletions house-robber/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
# O(n)
def rob(self, nums: list[int]) -> int:
if len(nums) <= 2:
return max(nums)
nums[2] += nums[0]
for i in range(3, len(nums)):
nums[i] += max(nums[i-3], nums[i-2])
return max(nums[-1], nums[-2])
13 changes: 13 additions & 0 deletions longest-consecutive-sequence/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
# O(n)
def longestConsecutive(self, nums: list[int]) -> int:
max_length = 0
nums_set = set(nums)
for n in nums_set:
if n - 1 not in nums_set:
length = 0
while n + length in nums_set:
length += 1
max_length = max(max_length, length)

return max_length
9 changes: 9 additions & 0 deletions top-k-frequent-elements/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from collections import Counter
import heapq


class Solution:
# O(nlogn)
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
ls = [(key, value) for key, value in Counter(nums).items()] # O(n)
return [key for _, key in heapq.nlargest(n=k, iterable=ls)] # O(nlogn)
5 changes: 5 additions & 0 deletions valid-palindrome/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
# O(n)
def isPalindrome(self, s: str) -> bool:
s = ''.join(ch.lower() for ch in s if ch.isalnum()) # O(n)
return s == s[::-1] # O(n)

0 comments on commit b8b7c86

Please sign in to comment.