Skip to content

Commit d44e0c8

Browse files
committed
✨ [128] Longest Consecutive Sequence
1 parent 81888b2 commit d44e0c8

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

128/my_solution.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestConsecutive = (nums) => {
6+
let set = new Set();
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
set.add(nums[i]);
10+
}
11+
12+
let maxCount = 0, sequenceMap = new Map();
13+
for (let val of set) {
14+
console.log(`val:${val}`)
15+
// NOTE: Only get the start of a sequence
16+
if (!set.has(val - 1)) {
17+
let nextVal = val + 0; // curr / next
18+
while (set.has(nextVal)) {
19+
sequenceMap.set(val, (sequenceMap.get(val) || 0) + 1);
20+
maxCount = Math.max(sequenceMap.get(val), maxCount);
21+
nextVal++;
22+
}
23+
}
24+
}
25+
26+
console.log(set)
27+
console.log(sequenceMap)
28+
return maxCount;
29+
};
30+
31+
let x = longestConsecutive([100, 4, 200, 1, 3, 2]);
32+
33+
console.log("Result")
34+
console.log(x)

128/solution.js

Whitespace-only changes.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [76. Minimum Window Substring](./76/)
3333
- [83. Remove Duplicates from Sorted List](./83/)
3434
- [121. Best Time to Buy and Sell Stock](./121/)
35+
- [128. Longest Consecutive Sequence](./128/)
3536
- [133. Clone Graph](./133/)
3637
- [125. Valid Palindrome](./125/)
3738
- [141. Linked List Cycle](./141/)
@@ -108,7 +109,7 @@ Batch create:
108109
NOTE: JS IS HERE
109110
-->
110111
```ssh
111-
chapter=417 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
112+
chapter=128 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
112113
```
113114
> then you can use `x` for quick debug.
114115

0 commit comments

Comments
 (0)