We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1af5cfb commit da56f83Copy full SHA for da56f83
my-submissions/m128 v2.py
@@ -1,19 +1,25 @@
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
- for val in vals: # O(n) looping
- if val - 1 in vals: # not start of sequence
11
- continue
12
13
- cnter = 1
14
- while val + cnter in vals :
15
- cnter += 1
+ while vals :
+ left = right = vals.pop()
+ curr = 1
+ while left - 1 in vals :
+ left -= 1
16
+ curr += 1
17
+ vals.remove(left)
18
+ while right + 1 in vals :
19
+ right += 1
20
21
+ vals.remove(right)
22
- maxx = max(maxx, cnter)
23
+ maxx = max(maxx, curr)
24
25
return maxx
0 commit comments