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 116161f commit 30a1246Copy full SHA for 30a1246
βlongest-consecutive-sequence/y00eunji.jsβ
@@ -0,0 +1,27 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+var longestConsecutive = function(nums) {
6
+ const numSet = new Set(nums);
7
+ let longestStreak = 0;
8
+
9
+ for (const num of numSet) {
10
+ // νμ¬ μ«μκ° μ°μ μνμ€μ μμμ μΈμ§ νμΈ
11
+ // μ¦, num-1μ΄ setμ μμ΄μΌ ν¨
12
+ if (!numSet.has(num - 1)) {
13
+ let currentNum = num;
14
+ let currentStreak = 1;
15
16
+ // νμ¬ μ«μμ μ°μλ λ€μ μ«μλ€μ μ°Ύμ
17
+ while (numSet.has(currentNum + 1)) {
18
+ currentNum += 1;
19
+ currentStreak += 1;
20
+ }
21
22
+ longestStreak = Math.max(longestStreak, currentStreak);
23
24
25
26
+ return longestStreak;
27
+};
0 commit comments