We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8414df7 commit bff73b3Copy full SHA for bff73b3
longest-consecutive-sequence/choidabom.ts
@@ -0,0 +1,25 @@
1
+// https://leetcode.com/problems/longest-consecutive-sequence/
2
+
3
+// TC: O(n)
4
+// SC: O(n)
5
6
+function longestConsecutive(nums: number[]): number {
7
+ const numSet = new Set(nums);
8
+ let maxLen = 0;
9
10
+ for (const num of numSet) {
11
+ if (!numSet.has(num - 1)) {
12
+ let currentNum = num;
13
+ let length = 1;
14
15
+ while (numSet.has(currentNum + 1)) {
16
+ currentNum += 1;
17
+ length += 1;
18
+ }
19
20
+ maxLen = Math.max(maxLen, length);
21
22
23
24
+ return maxLen;
25
+}
0 commit comments