File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @description
3
+ * time complexity: O(n log n)
4
+ * space complexity: O(n)
5
+ * runtime: 57ms
6
+ * νμ΄ λ°©λ²: μ€λ³΅μ μ κ±°νκ³ μ λ ¬ν λ€μμ μ°μλ μ«μμ κ°μλ₯Ό μΉ΄μ΄νΈνμ¬ μ΅λκ°μ λ°ννλ€.
7
+ * @param {number[] } nums
8
+ * @return {number }
9
+ */
10
+ const longestConsecutive = function ( nums ) {
11
+ if ( nums . length === 0 ) return 0 ;
12
+ const sortedNums = [ ...new Set ( nums ) ] . sort ( ( a , b ) => a - b ) ;
13
+
14
+ let maxConsecutiveCount = 1 ;
15
+ let currentConsecutiveCount = 1 ;
16
+
17
+ for ( let i = 1 ; i < sortedNums . length ; i += 1 ) {
18
+ if ( sortedNums [ i ] === sortedNums [ i - 1 ] + 1 ) {
19
+ currentConsecutiveCount += 1 ;
20
+ } else {
21
+ currentConsecutiveCount = 1 ;
22
+ }
23
+
24
+ maxConsecutiveCount = Math . max (
25
+ maxConsecutiveCount ,
26
+ currentConsecutiveCount
27
+ ) ;
28
+ }
29
+
30
+ return maxConsecutiveCount ;
31
+ } ;
You canβt perform that action at this time.
0 commit comments