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 a26c3ca commit 4f0c3baCopy full SHA for 4f0c3ba
โlongest-consecutive-sequence/byol-han.jsโ
@@ -0,0 +1,27 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+var longestConsecutive = function (nums) {
6
+ if (nums.length === 0) return 0;
7
+
8
+ const numSet = new Set(nums);
9
+ let longest = 0;
10
+ for (let num of numSet) {
11
+ // `num - 1`์ด ์์ผ๋ฉด ์๋ก์ด ์์ด์ ์์์
12
+ if (!numSet.has(num - 1)) {
13
+ let count = 1;
14
+ let currentNum = num;
15
16
+ // ์ฐ์๋ ์ซ์ ์ฐพ๊ธฐ
17
+ while (numSet.has(currentNum + 1)) {
18
+ currentNum++;
19
+ count++;
20
+ }
21
22
+ longest = Math.max(longest, count);
23
24
25
26
+ return longest;
27
+};
0 commit comments