-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path128_longestConsecutive.js
More file actions
44 lines (41 loc) · 1.02 KB
/
Copy path128_longestConsecutive.js
File metadata and controls
44 lines (41 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* https://leetcode.cn/problems/longest-consecutive-sequence
* 最长连续序列
* medium
*/
// O(n)? O(n) 有些极端用例会比较慢
var longestConsecutive = function(arr) {
const set = new Set(arr);
let maxCount = 0;
for (const num of set) {
if (!set.has(num - 1)) {
let current = num;
let count = 1;
while (set.has(current + 1)) {
++current;
++count;
}
maxCount = Math.max(maxCount, count);
}
}
return maxCount;
}
// 解法2 O(nlogn) O(1) <- 反而比较快
var longestConsecutive2 = function(arr) {
if (!arr.length) return 0;
arr.sort((x, y) => x - y);
let count = 1, maxCount = 1;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] === arr[i - 1]) {
continue;
} else if (arr[i] === arr[i - 1] + 1) {
maxCount = Math.max(maxCount, ++count);
} else {
count = 1;
}
}
return maxCount;
}
// ----
console.log(longestConsecutive([100, 4, 200, 1, 3, 2]));
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]));