Skip to content

Commit 694cf23

Browse files
committed
๐Ÿ“ Docs: solved #240
1 parent 2f982d3 commit 694cf23

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n log n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
4+
*
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var longestConsecutive = function(nums) {
9+
// ์˜ˆ์™ธ
10+
if (nums.length === 0) {
11+
return 0;
12+
}
13+
14+
const sortedArr = [...new Set(nums)].sort((a, b) => a - b);
15+
16+
let result = 1;
17+
let consecutive = 1;
18+
19+
for (let i = 0; i < sortedArr.length - 1; i++) {
20+
const curr = sortedArr[i];
21+
const next = sortedArr[i + 1];
22+
23+
if (next === curr + 1) {
24+
consecutive += 1;
25+
} else {
26+
consecutive = 1;
27+
}
28+
29+
result = Math.max(result, consecutive);
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
ย (0)