We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 63f4483 commit bd65038Copy full SHA for bd65038
1 file changed
β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