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 6f1cdad commit f0b55b2Copy full SHA for f0b55b2
longest-consecutive-sequence/HC-kang.ts
@@ -1,3 +1,24 @@
1
+/**
2
+ * https://leetcode.com/problems/longest-consecutive-sequence/
3
+ * T.C.: O(n)
4
+ * S.C.: O(n)
5
+ */
6
function longestConsecutive(nums: number[]): number {
- return 0;
7
+ const numSet = new Set(nums);
8
+ let max = 0;
9
+
10
+ for (const num of numSet) {
11
+ if (numSet.has(num - 1)) {
12
+ continue;
13
+ }
14
15
+ let count = 0;
16
+ while (numSet.has(num + count)) {
17
+ count++;
18
19
20
+ if (count > max) max = count;
21
22
23
+ return max;
24
}
0 commit comments