File tree 2 files changed +32
-8
lines changed
2 files changed +32
-8
lines changed Original file line number Diff line number Diff line change 3
3
* @return {number }
4
4
*/
5
5
const longestConsecutive = ( nums ) => {
6
- let set = new Set ( ) ;
6
+ let set = new Set ( ) , maxCount = 0 ;
7
7
8
8
for ( let i = 0 ; i < nums . length ; i ++ ) {
9
9
set . add ( nums [ i ] ) ;
10
10
}
11
11
12
- let maxCount = 0 , sequenceMap = new Map ( ) ;
13
12
for ( let val of set ) {
14
13
console . log ( `val:${ val } ` )
15
14
// NOTE: Only get the start of a sequence
16
15
if ( ! set . has ( val - 1 ) ) {
17
- let nextVal = val + 0 ; // curr / next
18
- while ( set . has ( nextVal ) ) {
19
- sequenceMap . set ( val , ( sequenceMap . get ( val ) || 0 ) + 1 ) ;
20
- maxCount = Math . max ( sequenceMap . get ( val ) , maxCount ) ;
21
- nextVal ++ ;
16
+ console . log ( "this does not have a before - is a head" )
17
+
18
+ // curr / next
19
+ let expand = 0 ;
20
+ while ( set . has ( val + expand ) ) {
21
+ expand ++ ;
22
22
}
23
+
24
+ maxCount = Math . max ( expand , maxCount ) ;
23
25
}
24
26
}
25
27
26
28
console . log ( set )
27
- console . log ( sequenceMap )
28
29
return maxCount ;
29
30
} ;
30
31
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ const longestConsecutive = ( nums ) => {
6
+ let set = new Set ( ) , maxCount = 0 ;
7
+
8
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
9
+ set . add ( nums [ i ] ) ;
10
+ }
11
+
12
+ for ( let val of set ) {
13
+ if ( ! set . has ( val - 1 ) ) {
14
+ let expand = 0 ;
15
+ while ( set . has ( val + expand ) ) {
16
+ expand ++ ;
17
+ }
18
+ maxCount = Math . max ( expand , maxCount ) ;
19
+ }
20
+ }
21
+
22
+ return maxCount ;
23
+ } ;
You can’t perform that action at this time.
0 commit comments