File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ซ์๋ค๋ก ๋ง๋ค ์ ์๋ ๊ฐ์ฅ ๊ธด ์ฐ์ ์์ด์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋ ํจ์
3+ * @param {number[] } nums
4+ * @return {number }
5+ */
6+ const longestConsecutive = function ( nums ) {
7+ const sorted = Array . from ( new Set ( nums ) ) . sort ( ( a , b ) => Number ( a ) - Number ( b ) ) ;
8+
9+ let maxLength = 0 ;
10+ let currentSequenceLength = 0 ;
11+
12+ for ( let i = 0 ; i < sorted . length ; i ++ ) {
13+ if ( i === 0 ) {
14+ maxLength = 1 ;
15+ currentSequenceLength = 1 ;
16+ continue ;
17+ }
18+
19+ if ( sorted [ i ] === sorted [ i - 1 ] + 1 ) {
20+ currentSequenceLength += 1 ;
21+ } else {
22+ currentSequenceLength = 1 ;
23+ }
24+
25+ maxLength = Math . max ( maxLength , currentSequenceLength ) ;
26+ }
27+
28+ return maxLength ;
29+ } ;
30+
31+ // ์๊ฐ๋ณต์ก๋: O(n)
32+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
You canโt perform that action at this time.
0 commit comments