Skip to content

Commit 7c26233

Browse files
committed
longest-consecutive-sequence
1 parent cbd7d6e commit 7c26233

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
μ‹œκ°„λ³΅μž‘λ„: O(n) - λͺ¨λ“  μš”μ†Œμ— λŒ€ν•΄ μ΅œλŒ€ ν•œ λ²ˆμ”©λ§Œ κ²€μ‚¬ν•˜κΈ° λ•Œλ¬Έ
3+
- Set을 μ‚¬μš©ν•˜μ—¬ O(1) μ‹œκ°„μ— μš”μ†Œ 쑴재 μ—¬λΆ€ 확인 κ°€λŠ₯
4+
- 각 μˆ«μžλŠ” 연속 μˆ˜μ—΄μ˜ μ‹œμž‘μ μΈ κ²½μš°μ—λ§Œ while 루프λ₯Ό μ‹€ν–‰
5+
-> λͺ¨λ“  μš”μ†Œμ— λŒ€ν•΄ while λ£¨ν”„μ˜ 총 반볡 νšŸμˆ˜λŠ” 전체 μš”μ†Œ 수λ₯Ό μ΄ˆκ³Όν•˜μ§€ μ•ŠμŒ
6+
*/
7+
/**
8+
* @param {number[]} nums
9+
* @return {number}
10+
*/
11+
12+
var longestConsecutive = function (nums) {
13+
const numSet = new Set(nums);
14+
let longest = 0;
15+
16+
for (let n of [...numSet]) {
17+
if (!numSet.has(n - 1)) {
18+
let length = 0;
19+
while (numSet.has(n + length)) {
20+
length += 1;
21+
}
22+
longest = Math.max(length, longest);
23+
}
24+
}
25+
return longest;
26+
};

0 commit comments

Comments
Β (0)