Skip to content

Commit 01af48d

Browse files
meteorqz6claude
andcommitted
longest consecutive sequence solution
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 265c829 commit 01af48d

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 시간 복잡도: O(n log n)
2+
// 공간 복잡도: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var longestConsecutive = function (nums) {
9+
let cnt = 0;
10+
const set = new Set(nums);
11+
const sortArray = Array.from(set).sort((a, b) => a - b);
12+
const cntArray = [];
13+
14+
for (let i = 0; i < sortArray.length; i++) {
15+
if (sortArray[i] + 1 !== sortArray[i + 1]) {
16+
cntArray.push(cnt + 1);
17+
cnt = 0;
18+
} else {
19+
cnt += 1;
20+
}
21+
}
22+
23+
if (nums.length === 0) return 0;
24+
else return Math.max(...cntArray);
25+
};

0 commit comments

Comments
 (0)