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 c56b727 commit 1053604Copy full SHA for 1053604
1 file changed
โlongest-consecutive-sequence/HerrineKim.jsโ
@@ -0,0 +1,31 @@
1
+// ์๊ฐ๋ณต์ก๋: O(n)
2
+
3
+/**
4
+ * @param {number[]} nums
5
+ * @return {number}
6
+ */
7
+var longestConsecutive = function (nums) {
8
+ // Set์ ์ฌ์ฉํด ์ค๋ณต ์ ๊ฑฐ
9
+ const numSet = new Set(nums);
10
+ let longestStreak = 0;
11
12
+ // ๊ฐ ์ซ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ฐ์ ์ํ์ค๋ฅผ ํ์
13
+ for (let num of numSet) {
14
+ // num์ด ์ํ์ค์ ์์์ ์ธ ๊ฒฝ์ฐ๋ง ํ์
15
+ if (!numSet.has(num - 1)) {
16
+ let currentNum = num;
17
+ let currentStreak = 1;
18
19
+ // ํ์ฌ ์ํ์ค๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ๊ธธ์ด ๊ณ์ฐ
20
+ while (numSet.has(currentNum + 1)) {
21
+ currentNum++;
22
+ currentStreak++;
23
+ }
24
25
+ // ์ต๋ ๊ธธ์ด๋ฅผ ์ ๋ฐ์ดํธ
26
+ longestStreak = Math.max(longestStreak, currentStreak);
27
28
29
30
+ return longestStreak;
31
+};
0 commit comments