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+ // ์๊ฐ๋ณต์ก๋: O(n)
2+
3+ /**
4+ * @param {number[] } nums
5+ * @return {number }
6+ */
7+ var longestConsecutive = function ( nums ) {
8+ // Set์ ์ฌ์ฉํด ์ค๋ณต ์ ๊ฑฐ
9+ const numSet = new Set ( nums ) ;
10+ let longestStreak = 0 ;
11+
12+ // ๊ฐ ์ซ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ฐ์ ์ํ์ค๋ฅผ ํ์
13+ for ( let num of numSet ) {
14+ // num์ด ์ํ์ค์ ์์์ ์ธ ๊ฒฝ์ฐ๋ง ํ์
15+ if ( ! numSet . has ( num - 1 ) ) {
16+ let currentNum = num ;
17+ let currentStreak = 1 ;
18+
19+ // ํ์ฌ ์ํ์ค๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ๊ธธ์ด ๊ณ์ฐ
20+ while ( numSet . has ( currentNum + 1 ) ) {
21+ currentNum ++ ;
22+ currentStreak ++ ;
23+ }
24+
25+ // ์ต๋ ๊ธธ์ด๋ฅผ ์
๋ฐ์ดํธ
26+ longestStreak = Math . max ( longestStreak , currentStreak ) ;
27+ }
28+ }
29+
30+ return longestStreak ;
31+ } ;
You canโt perform that action at this time.
0 commit comments