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 694cf23 commit 05ffb72Copy full SHA for 05ffb72
longest-consecutive-sequence/jangwonyoon.js
@@ -1,4 +1,6 @@
1
/**
2
+ * solve 1
3
+ *
4
* 시간 복잡도: O(n log n)
5
* 공간 복잡도: O(n)
6
*
@@ -31,3 +33,28 @@ var longestConsecutive = function(nums) {
31
33
32
34
return result;
35
};
36
+
37
+/**
38
+ * solve 2
39
40
+ * 시간 복잡도: O(n)
41
+ * 공간 복잡도: O(n)
42
43
+ * @param {number[]} nums
44
+ * @return {number}
45
+ */
46
+var longestConsecutive = function(nums) {
47
+ let longest = 0;
48
+ const numSet = new Set(nums);
49
50
+ for (const num of numSet) {
51
+ if (numSet.has(num - 1)) continue;
52
+ let length = 1;
53
54
+ while (numSet.has(num + length)) length++;
55
56
+ longest = Math.max(length, longest);
57
+ }
58
59
+ return longest;
60
+}
0 commit comments