Skip to content

Commit 94aaf92

Browse files
committed
Try different optimization of m128
1 parent 46f5ed9 commit 94aaf92

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

my-submissions/m128.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# O(n) solution babyyyy
2-
31
class Solution:
42
def longestConsecutive(self, nums: List[int]) -> int:
53
if len(nums) == 0 :
64
return 0
7-
5+
86
vals = set(nums) # O(n) creation, O(1) lookups
97

108
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
9+
while vals :
10+
left = right = vals.pop()
11+
curr = 1
12+
while left - 1 in vals :
13+
left -= 1
14+
curr += 1
15+
vals.remove(left)
16+
while right + 1 in vals :
17+
right += 1
18+
curr += 1
19+
vals.remove(right)
1820

19-
maxx = max(maxx, cnter)
21+
maxx = max(maxx, curr)
2022

2123
return maxx

0 commit comments

Comments
 (0)