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 d0956f1 commit de9ff87Copy full SHA for de9ff87
1 file changed
βlongest-consecutive-sequence/anniemon.jsβ
@@ -0,0 +1,30 @@
1
+/**
2
+ * μκ° λ³΅μ‘λ:
3
+ * setμ μμ μ€ μ°μνλ μνμ€μ 첫λ²μ§Έ μ«μμΌ λλ§
4
+ * while 루νλ₯Ό μ€ν
5
+ * λ°λΌμ μμλΉ μ΅λ 1ν μν
6
+ * μ¦, μκ° λ³΅μ‘λλ O(n)
7
+ * κ³΅κ° λ³΅μ‘λ:
8
+ * setμ μ€λ³΅μ΄ μμ κ²½μ° μ΅λ O(n)λ₯Ό μ°¨μ§ν¨
9
+ * μ¦, κ³΅κ° λ³΅μ‘λλ O(n)
10
+ */
11
12
+ * @param {number[]} nums
13
+ * @return {number}
14
15
+var longestConsecutive = function (nums) {
16
+ const set = new Set(nums);
17
+ let res = 0;
18
+ for (const n of set) {
19
+ let seqLen = 0, target = n;
20
+ const isSeqStart = !set.has(target - 1);
21
+ if (isSeqStart) {
22
+ while (set.has(target)) {
23
+ target++;
24
+ seqLen++;
25
+ }
26
27
+ res = Math.max(seqLen, res);
28
29
+ return res;
30
+};
0 commit comments