Skip to content

Commit bff73b3

Browse files
committed
feat: longest-consecutive-sequence
1 parent 8414df7 commit bff73b3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// https://leetcode.com/problems/longest-consecutive-sequence/
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
6+
function longestConsecutive(nums: number[]): number {
7+
const numSet = new Set(nums);
8+
let maxLen = 0;
9+
10+
for (const num of numSet) {
11+
if (!numSet.has(num - 1)) {
12+
let currentNum = num;
13+
let length = 1;
14+
15+
while (numSet.has(currentNum + 1)) {
16+
currentNum += 1;
17+
length += 1;
18+
}
19+
20+
maxLen = Math.max(maxLen, length);
21+
}
22+
}
23+
24+
return maxLen;
25+
}

0 commit comments

Comments
 (0)