Skip to content

Commit 3630dfb

Browse files
committed
Add prompt and solution for LC 128 Longest Consecutive Sequence
1 parent 8eeb2f7 commit 3630dfb

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

125. Valid Palindrome.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
* @return {boolean}
2828
*/
2929
const isPalindrome = (s) => {
30-
s = s.replace(/[^a-z0-9]/gi, "").toLowerCase();
31-
return s === s.split("").reverse().join("");
30+
s = s.toLowerCase().replace(/[^a-z0-9]/g, "");
31+
return s === s.split("").reverse().join("");
3232
};

128. Longest Consecutive Sequence.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
4+
5+
You must write an algorithm that runs in O(n) time.
6+
7+
Example 1:
8+
Input: nums = [100,4,200,1,3,2]
9+
Output: 4
10+
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
11+
12+
Example 2:
13+
Input: nums = [0,3,7,2,5,8,4,6,0,1]
14+
Output: 9
15+
16+
Constraints:
17+
0 <= nums.length <= 105
18+
-109 <= nums[i] <= 109
19+
20+
*/
21+
22+
/**
23+
* @param {number[]} nums
24+
* @return {number}
25+
*/
26+
const longestConsecutive = (nums) => {
27+
if (nums.length < 2) return nums.length;
28+
29+
const set = new Set(nums);
30+
let maxLength = 0;
31+
32+
for (const num of set) {
33+
if (set.has(num - 1)) continue;
34+
35+
let currNum = num;
36+
let currLength = 1;
37+
38+
while (set.has(currNum + 1)) {
39+
currNum += 1;
40+
currLength += 1;
41+
}
42+
43+
maxLength = Math.max(maxLength, currLength);
44+
}
45+
46+
return maxLength;
47+
};

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LeetCode Profile: https://leetcode.com/timothyshores/
2121
|[111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|Easy|[JavaScript](111.%20Minimum%20Depth%20of%20Binary%20Tree.js)
2222
|[121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|Easy|[JavaScript](121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock.js)
2323
|[125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|Easy|[JavaScript](125.%20Valid%20Palindrome.js)
24+
|[128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)|Medium|[JavaScript](128.%20Longest%20Consecutive%20Sequence.js)
2425
|[136. Single Number](https://leetcode.com/problems/single-number/)|Easy|[JavaScript](136.%20Single%20Number.js)
2526
|[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)|Easy|[JavaScript](144.%20Binary%20Tree%20Preorder%20Traversal.js)
2627
|[155. Min Stack](https://leetcode.com/problems/min-stack/)|Medium|[JavaScript](155.%20Min%20Stack.js)
@@ -54,7 +55,7 @@ LeetCode Profile: https://leetcode.com/timothyshores/
5455
|[896. Monotonic Array](https://leetcode.com/problems/monotonic-array/)|Easy|[JavaScript](896.%20Monotonic%20Array.js)
5556
|[938. Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|Medium|[JavaScript](938.%20Range%20Sum%20of%20BST.js)
5657
|[965. Univalued Binary Tree.js](https://leetcode.com/problems/univalued-binary-tree/)|Easy|[JavaScript](965.%20Univalued%20Binary%20Tree.js)
57-
|[1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/)|Easy|[JavaScript](1047.%20Remove%20All%20Adjacent%20Duplicates%20In%20String.js
58+
|[1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/)|Easy|[JavaScript](1047.%20Remove%20All%20Adjacent%20Duplicates%20In%20String.js)
5859
|[1108. Defanging An IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|Easy|[JavaScript](1108.%20Defanging%20An%20IP%20Address.js)
5960
|[1170. Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|Medium|[JavaScript](1170.%20Compare%20Strings%20by%20Frequency%20of%20the%20Smallest%20Character.js)
6061
|[1207. Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|Easy|[JavaScript](1207.%20Unique%20Number%20of%20Occurrences.js)

0 commit comments

Comments
 (0)