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