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