Skip to content

Commit f0b55b2

Browse files
committed
Feat: 128. Longest Consecutive Sequence
1 parent 6f1cdad commit f0b55b2

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/longest-consecutive-sequence/
3+
* T.C.: O(n)
4+
* S.C.: O(n)
5+
*/
16
function longestConsecutive(nums: number[]): number {
2-
return 0;
7+
const numSet = new Set(nums);
8+
let max = 0;
9+
10+
for (const num of numSet) {
11+
if (numSet.has(num - 1)) {
12+
continue;
13+
}
14+
15+
let count = 0;
16+
while (numSet.has(num + count)) {
17+
count++;
18+
}
19+
20+
if (count > max) max = count;
21+
}
22+
23+
return max;
324
}

0 commit comments

Comments
 (0)