diff --git a/contains-duplicate/jeldo.py b/contains-duplicate/jeldo.py new file mode 100644 index 000000000..9289d2cd8 --- /dev/null +++ b/contains-duplicate/jeldo.py @@ -0,0 +1,4 @@ +class Solution: + # O(n) + def containsDuplicate(self, nums: list[int]) -> bool: + return len(nums) != len(set(nums)) # O(n) diff --git a/house-robber/jeldo.py b/house-robber/jeldo.py new file mode 100644 index 000000000..f0d53f73c --- /dev/null +++ b/house-robber/jeldo.py @@ -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]) diff --git a/longest-consecutive-sequence/jeldo.py b/longest-consecutive-sequence/jeldo.py new file mode 100644 index 000000000..c645fe334 --- /dev/null +++ b/longest-consecutive-sequence/jeldo.py @@ -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 diff --git a/top-k-frequent-elements/jeldo.py b/top-k-frequent-elements/jeldo.py new file mode 100644 index 000000000..72070aeff --- /dev/null +++ b/top-k-frequent-elements/jeldo.py @@ -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) diff --git a/valid-palindrome/jeldo.py b/valid-palindrome/jeldo.py new file mode 100644 index 000000000..f72d84fed --- /dev/null +++ b/valid-palindrome/jeldo.py @@ -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)