File tree Expand file tree Collapse file tree 5 files changed +40
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ # O(n)
3+ def containsDuplicate (self , nums : list [int ]) -> bool :
4+ return len (nums ) != len (set (nums )) # O(n)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ # O(n)
3+ def rob (self , nums : list [int ]) -> int :
4+ if len (nums ) <= 2 :
5+ return max (nums )
6+ nums [2 ] += nums [0 ]
7+ for i in range (3 , len (nums )):
8+ nums [i ] += max (nums [i - 3 ], nums [i - 2 ])
9+ return max (nums [- 1 ], nums [- 2 ])
Original file line number Diff line number Diff line change 1+ class Solution :
2+ # O(n)
3+ def longestConsecutive (self , nums : list [int ]) -> int :
4+ max_length = 0
5+ nums_set = set (nums )
6+ for n in nums_set :
7+ if n - 1 not in nums_set :
8+ length = 0
9+ while n + length in nums_set :
10+ length += 1
11+ max_length = max (max_length , length )
12+
13+ return max_length
Original file line number Diff line number Diff line change 1+ from collections import Counter
2+ import heapq
3+
4+
5+ class Solution :
6+ # O(nlogn)
7+ def topKFrequent (self , nums : list [int ], k : int ) -> list [int ]:
8+ ls = [(key , value ) for key , value in Counter (nums ).items ()] # O(n)
9+ return [key for _ , key in heapq .nlargest (n = k , iterable = ls )] # O(nlogn)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ # O(n)
3+ def isPalindrome (self , s : str ) -> bool :
4+ s = '' .join (ch .lower () for ch in s if ch .isalnum ()) # O(n)
5+ return s == s [::- 1 ] # O(n)
You can’t perform that action at this time.
0 commit comments