1
- //////////////////////////////////////////////////////////////////////////////
2
- // Linear Search With A Hash Map
3
- // Time: O(n)
4
- // Space: O(n)
5
- // This solution only makes one pass over the `nums` array and is the highest
6
- // performing solution.
7
- //////////////////////////////////////////////////////////////////////////////
8
-
9
1
/**
2
+ * Brute Force
3
+ * Greedy - Max Score
4
+ * Time O (N^3) | Space O(1)
5
+ * https://leetcode.com/problems/longest-consecutive-sequence/
10
6
* @param {number[] } nums
11
7
* @return {number }
12
8
*/
13
- function longestConsecutive ( nums ) {
14
- if ( ! nums . length ) {
15
- return 0 ;
16
- }
9
+ var longestConsecutive = ( nums , maxScore = 0 ) => {
10
+ for ( const num of nums ) { /* Time O(N) */
11
+ let [ currNum , score ] = [ num , 1 ] ;
17
12
18
- const map = Object . create ( null ) ;
19
- let max = 0 ;
20
-
21
- for ( const num of nums ) {
22
- if ( num in map ) {
23
- continue ;
13
+ while ( isStreak ( nums , ( currNum + 1 ) ) ) { /* Time O(N * N) */
14
+ currNum ++ ;
15
+ score ++ ;
24
16
}
25
17
26
- const prev = num - 1 ;
27
- const next = num + 1 ;
28
- let len = 1 ;
29
-
30
- if ( prev in map ) {
31
- if ( next in map ) {
32
- len += map [ prev ] + map [ next ] ;
33
- map [ prev - map [ prev ] + 1 ] = len ;
34
- map [ next + map [ next ] - 1 ] = len ;
35
- } else {
36
- len += map [ prev ] ;
37
- ++ map [ prev - map [ prev ] + 1 ] ;
38
- }
39
- } else if ( next in map ) {
40
- len += map [ next ] ;
41
- ++ map [ next + map [ next ] - 1 ] ;
42
- }
43
- map [ num ] = len ;
44
- max = Math . max ( max , len ) ;
18
+ maxScore = Math . max ( maxScore , score ) ;
45
19
}
46
20
47
- return max ;
21
+ return maxScore ;
48
22
}
49
23
50
- //////////////////////////////////////////////////////////////////////////////
51
- // Linear Search With A Hash Set
52
- // Time: O(n)
53
- // Space: O(n)
54
- // This solution does three passes over the `nums` array. A first pass to
55
- // setup the hash set. A second pass to find the numbers that mark the
56
- // beginning of a sequence. A third pass to calculate the length of each
57
- // sequence. The nested `while` loop does not cause quadratic calculations as
58
- // it is only initiated on the first number of each sequence.
59
- //////////////////////////////////////////////////////////////////////////////
24
+ const isStreak = ( nums , num ) => {
25
+ for ( let i = 0 ; i < nums . length ; i ++ ) { /* Time O(N) */
26
+ const isEqual = nums [ i ] === num
27
+ if ( isEqual ) return true ;
28
+ }
29
+
30
+ return false ;
31
+ }
60
32
61
33
/**
34
+ * Sort - HeapSort Space O(1) | QuickSort Space O(log(K))
35
+ * Greedy - Max Score
36
+ * Time O (N * log(N)) | Space O(1)
37
+ * https://leetcode.com/problems/longest-consecutive-sequence/
62
38
* @param {number[] } nums
63
39
* @return {number }
64
40
*/
65
- function longestConsecutive ( nums ) {
66
- let len = nums . length ;
67
- if ( ! len ) {
68
- return 0 ;
41
+ var longestConsecutive = ( nums ) => {
42
+ if ( ! nums . length ) return 0 ;
43
+
44
+ nums . sort ( ( a , b ) => a - b ) ; /* Time O(N * log(N)) | Space O(1 || log(N)) */
45
+
46
+ return search ( nums ) ; /* Time O(N) */
47
+ }
48
+
49
+ const search = ( nums ) => {
50
+ let [ maxScore , score ] = [ 1 , 1 ] ;
51
+
52
+ for ( let i = 1 ; i < nums . length ; i ++ ) { /* Time O(N) */
53
+ const isPrevDuplicate = nums [ i - 1 ] === nums [ i ]
54
+ if ( isPrevDuplicate ) continue
55
+
56
+ const isStreak = nums [ i ] === ( ( nums [ i - 1 ] ) + 1 )
57
+ if ( isStreak ) { score ++ ; continue ; }
58
+
59
+ maxScore = Math . max ( maxScore , score ) ;
60
+ score = 1 ;
69
61
}
70
- const set = new Set ( nums ) ;
71
- let max = 0 ;
72
62
73
- for ( let i = 0 ; i < nums . length ; i ++ ) {
74
- const num = nums [ i ] ;
63
+ return Math . max ( maxScore , score ) ;
64
+ }
75
65
76
- if ( set . has ( num - 1 ) ) {
77
- continue ;
78
- }
66
+ /**
67
+ * Hash Set - Intelligent Sequence
68
+ * Greedy - Max Score
69
+ * Time O (N) | Space O(N)
70
+ * https://leetcode.com/problems/longest-consecutive-sequence/
71
+ * @param {number[] } nums
72
+ * @return {number }
73
+ */
74
+ var longestConsecutive = ( nums , maxScore = 0 ) => {
75
+ const numSet = new Set ( nums ) ; /* Time O(N) | Space O(N) */
79
76
80
- let currentMax = 1 ;
81
- while ( set . has ( num + currentMax ) ) {
82
- currentMax ++ ;
83
- }
77
+ for ( const num of [ ...numSet ] ) { /* Time O(N) */
78
+ const prevNum = num - 1 ;
84
79
85
- if ( currentMax > max ) {
86
- max = currentMax ;
87
- }
88
- if ( max > len / 2 ) {
89
- break ;
80
+ if ( numSet . has ( prevNum ) ) continue ; /* Time O(N) */
81
+
82
+ let [ currNum , score ] = [ num , 1 ] ;
83
+
84
+ const isStreak = ( ) => numSet . has ( currNum + 1 )
85
+ while ( isStreak ( ) ) { /* Time O(N) */
86
+ currNum ++ ;
87
+ score ++ ;
90
88
}
89
+
90
+ maxScore = Math . max ( maxScore , score ) ;
91
91
}
92
92
93
- return max ;
94
- }
93
+ return maxScore ;
94
+ }
0 commit comments