Skip to content

Commit cb600d6

Browse files
committed
add solution : 128. Longest Consecutive Sequence
1 parent 90b6bce commit cb600d6

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

โ€Žlongest-common-subsequence/mmyeon.js

-46
This file was deleted.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
*
3+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
4+
* - ์ค‘๋ณต ์ˆซ์ž ์ œ๊ฑฐํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ Set ์‚ฌ์šฉ
5+
* - ์—ฐ์†๋œ ์ˆซ์ž์˜ ์‹œ์ž‘์ (ํ˜„์žฌ ์ˆซ์ž๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ)๋ฅผ ์ฐพ์•„์„œ ์—ฐ์†๋œ ๊ธธ์ด ์นด์šดํŠธ
6+
* - ์ตœ๋Œ€ ๊ธธ์ด ๊ฐฑ์‹ 
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
9+
* - Set ์ƒ์„ฑ, ์ˆซ์ž ํƒ์ƒ‰ ๋ชจ๋‘ O(n)
10+
*
11+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
12+
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ, nums ๋ฐฐ์—ด ํฌ๊ธฐ๋งŒํผ set์— ์ €์žฅํ•˜๋‹ˆ๊นŒ O(n)
13+
*/
14+
function longestConsecutive(nums: number[]): number {
15+
const numSet = new Set(nums);
16+
let longestLength = 0;
17+
18+
for (const num of numSet) {
19+
if (!numSet.has(num - 1)) {
20+
let currentLength = 1;
21+
let currentNum = num + 1;
22+
23+
while (numSet.has(currentNum)) {
24+
currentLength++;
25+
currentNum++;
26+
}
27+
28+
longestLength = Math.max(longestLength, currentLength);
29+
}
30+
}
31+
32+
return longestLength;
33+
}

0 commit comments

Comments
ย (0)