File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ๋ถ๋ฅ๋์ง ์์ ์ ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ง ๋, ๊ฐ์ฅ ๊ธด ์ฐ์ ์์ ์ํ์ค์ ๊ธธ์ด๋ฅผ ๋ฆฌํดํ๋ ๋ฌธ์
3+ * ์กฐ๊ฑด: ์๊ฐ๋ณต์ก๋ O(n)
4+ *
5+ * ์ ๊ทผ ๋ฐฉ์:
6+ * (1) ๋น ๋ฐฐ์ด์ด๋ฉด 0 ๋ฐํํ๊ณ ๋น ๋ฅธ ํ์ถ
7+ * (2) Set์ ์ฌ์ฉํ์ฌ ์ค๋ณต ์ ๊ฑฐ ๋ฐ ๋น ๋ฅธ ์กฐํ
8+ * (3) Set์ ์ํํ๋ฉด์ ์ฐ์ ์ํ์ค์ ์์์ ์ธ์ง ํ์ธ(์์์ ๋ง ์ฒ๋ฆฌํด์ค)
9+ * (4) ์์์ ์ด๋ฉด ์ฐ์๋๋ ์ซ์๋ฅผ ์ฐพ์ ๊ธธ์ด ๊ณ์ฐํ๊ณ
10+ * (5) ์ต๋ ๊ธธ์ด ๊ฐฑ์ ํด์ค ํ ๋ฆฌํด
11+ *
12+ * /
13+
14+ /**
15+ * @param {number[] } nums
16+ * @return {number }
17+ */
18+ var longestConsecutive = function ( nums ) {
19+ if ( nums . length === 0 ) return 0 ;
20+
21+ const numSet = new Set ( nums ) ;
22+ let maxLength = 0 ;
23+
24+ for ( const num of numSet ) {
25+ if ( ! numSet . has ( num - 1 ) ) {
26+ let currentNum = num ;
27+ let currentLength = 1 ;
28+
29+ while ( numSet . has ( currentNum + 1 ) ) {
30+ currentNum ++ ;
31+ currentLength ++ ;
32+ }
33+
34+ maxLength = Math . max ( maxLength , currentLength ) ;
35+ }
36+ }
37+
38+ return maxLength ;
39+ } ;
You canโt perform that action at this time.
0 commit comments