We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 46f5ed9 commit 94aaf92Copy full SHA for 94aaf92
my-submissions/m128.py
@@ -1,21 +1,23 @@
1
-# O(n) solution babyyyy
2
-
3
class Solution:
4
def longestConsecutive(self, nums: List[int]) -> int:
5
if len(nums) == 0 :
6
return 0
7
+
8
vals = set(nums) # O(n) creation, O(1) lookups
9
10
maxx = 0
11
- for val in vals: # O(n) looping
12
- if val - 1 in vals: # not start of sequence
13
- continue
14
15
- cnter = 1
16
- while val + cnter in vals :
17
- cnter += 1
+ while vals :
+ left = right = vals.pop()
+ curr = 1
+ while left - 1 in vals :
+ left -= 1
+ curr += 1
+ vals.remove(left)
+ while right + 1 in vals :
+ right += 1
18
19
+ vals.remove(right)
20
- maxx = max(maxx, cnter)
21
+ maxx = max(maxx, curr)
22
23
return maxx
0 commit comments