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 ff227d7 commit e0d74daCopy full SHA for e0d74da
1 file changed
โlongest-consecutive-sequence/Cyjin-jani.jsโ
@@ -0,0 +1,22 @@
1
+var longestConsecutive = function (nums) {
2
+ // ์ซ์ ์กด์ฌ ์ ๋ฌด๋ฅผ ํ์ธํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ ์ธํ
3
+ const dataSet = new Set(nums);
4
+ let answer = 0;
5
+
6
+ // ์ ์ฒด ์ซ์๋ฅผ ์ํํ๋ฉด์ ์์์ ์ ํ์ธ
7
+ for (let num of dataSet) {
8
+ // num - 1์ด dataSet์ ์๋์ง ํ์ธ. ์๋ค๋ฉด ์์์ ์ด ์๋๋ฏ๋ก ํจ์ค
9
+ if (!dataSet.has(num - 1)) {
10
+ // ์๋ค๋ฉด ์์์ . ์ฌ๊ธฐ์๋ถํฐ ์ฐ์๋ ์ซ์๊ฐ ์ผ๋ง๋ ์๋์ง ์นด์ดํ .
11
+ let count = 1;
12
+ let target = num;
13
+ while (dataSet.has(target + 1)) {
14
+ count++;
15
+ target++;
16
+ }
17
+ // longest๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ฏ๋ก max๋ก ๋ ๊ธด ๋ต์ ํ๋จ
18
+ answer = Math.max(count, answer);
19
20
21
+ return answer;
22
+};
0 commit comments