File tree 4 files changed +51
-23
lines changed
4 files changed +51
-23
lines changed Original file line number Diff line number Diff line change
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
18
+
19
+ maxx = max (maxx , cnter )
20
+
21
+ return maxx
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def longestConsecutive (self , nums : List [int ]) -> int :
3
+ if len (nums ) == 0 :
4
+ return 0
5
+
6
+ vals = set (nums ) # O(n) creation, O(1) lookups
7
+
8
+ maxx = 0
9
+ for val in vals : # O(n) looping
10
+ if val - 1 in vals : # not start of sequence
11
+ continue
12
+
13
+ cnter = 1
14
+ while val + cnter in vals :
15
+ cnter += 1
16
+
17
+ maxx = max (maxx , cnter )
18
+
19
+ return maxx
Original file line number Diff line number Diff line change
1
+ ## m128 v1
2
+
3
+ My initial attempt that was successful but inefficient. Achieved around a 350ms runtime.
4
+
5
+ ## m128 java
6
+
7
+ Second attempt that was much more successful and achieved a reasonable runtim
8
+
9
+ ## m128 v2
10
+
11
+ 2nd python attempt that implemented a set removal optimization to save redundancy that brought the python version to the same runtime level as java.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments