Skip to content

Commit 2a7b029

Browse files
committed
longest-consecutive-sequence time out
1 parent 85c0c33 commit 2a7b029

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+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
if (nums.length == 0) return 0;
7+
let answer = 0;
8+
let numsSet = new Set(nums);
9+
const N = nums.length;
10+
11+
for (let i = 0; i < N; i++) {
12+
let temp = nums[i];
13+
let length = 1;
14+
if (!numsSet.has(temp - 1)) {
15+
while (numsSet.has(temp + 1)) {
16+
length += 1;
17+
temp += 1;
18+
}
19+
// console.log(length)
20+
answer = Math.max(length, answer);
21+
}
22+
}
23+
24+
return answer;
25+
};

0 commit comments

Comments
 (0)