Skip to content

Commit 45241aa

Browse files
committed
longest-consecutive-seuence solution
1 parent b8d9436 commit 45241aa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function(nums) {
6+
if (nums.length === 0) return 0;
7+
8+
const numSet = new Set(nums);
9+
let maxLength = 0;
10+
11+
for (let num of numSet) {
12+
// 연속 μˆ˜μ—΄μ˜ μ‹œμž‘μ μΈμ§€ 확인
13+
if (!numSet.has(num - 1)) {
14+
let currentNum = num;
15+
let currentLength = 1;
16+
17+
// μ—°μ†λœ 숫자 μžˆλŠ” λ™μ•ˆ 증가
18+
while (numSet.has(currentNum + 1)) {
19+
currentNum += 1;
20+
currentLength += 1;
21+
}
22+
23+
maxLength = Math.max(maxLength, currentLength);
24+
}
25+
}
26+
27+
return maxLength;
28+
};

0 commit comments

Comments
Β (0)