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-
91/**
2+ * Brute Force
3+ * Greedy - Max Score
4+ * Time O (N^3) | Space O(1)
5+ * https://leetcode.com/problems/longest-consecutive-sequence/
106 * @param {number[] } nums
117 * @return {number }
128 */
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 ] ;
1712
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 ++ ;
2416 }
2517
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 ) ;
4519 }
4620
47- return max ;
21+ return maxScore ;
4822}
4923
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+ }
6032
6133/**
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/
6238 * @param {number[] } nums
6339 * @return {number }
6440 */
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 ;
6961 }
70- const set = new Set ( nums ) ;
71- let max = 0 ;
7262
73- for ( let i = 0 ; i < nums . length ; i ++ ) {
74- const num = nums [ i ] ;
63+ return Math . max ( maxScore , score ) ;
64+ }
7565
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) */
7976
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 ;
8479
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 ++ ;
9088 }
89+
90+ maxScore = Math . max ( maxScore , score ) ;
9191 }
9292
93- return max ;
94- }
93+ return maxScore ;
94+ }
0 commit comments