-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #646 from Jeldo/main
[jeldo3] Week1
- Loading branch information
Showing
5 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |