Skip to content

Commit 905230d

Browse files
feat: longest-consecutive-sequence
1 parent a3da5a1 commit 905230d

File tree

1 file changed

+86
-17
lines changed

1 file changed

+86
-17
lines changed
Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,99 @@
1+
12
/**
23
* ์ •์ˆ˜ ๋ฐฐ์—ด nums
34
* ๊ฐ€์žฅ ๋งŽ์ด ์—ฐ์†๋˜๋Š” ์š”์†Œ์˜ ๊ธธ์ด ๋ฆฌํ„ด.
45
* O(n) ์‹œ๊ฐ„์•ˆ์— ๋Œ์•„๊ฐ€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉํ• ๊ฒƒ.
56
*/
7+
/*์šฐ์„  ์ฒ˜์Œ ํ‘ผ ๋ฐฉ๋ฒ•์€*/
8+
9+
var longestConsecutive = function(nums) {
10+
let maxCount = 0;
11+
nums = [...nums].sort((a,b)=>a-b);
12+
for(let i = 0;i < nums.length;i++){
13+
let current = nums[i];
14+
let count = 1;
15+
for(let j = i+1;j<nums.length;j++){
16+
if(current+1 == nums[j]){
17+
current = nums[j]
18+
count +=1;
19+
}else if(current == nums[j]){
20+
continue;
21+
}
22+
}
23+
maxCount = Math.max(maxCount,count)
24+
count = 1;
25+
}
26+
return maxCount
27+
};
28+
29+
30+
/*์‹œ๊ฐ„๋ณต์žก๋„:
31+
2์ค‘for๋ฌธ+sort
32+
n^2 + nlogn => n^2.
33+
๊ณต๊ฐ„๋ณต์žก๋„:
34+
nums๋Š” ์ดˆ๊ธฐํ™”ํ–ˆ๊ณ ,
35+
๊ธฐํƒ€ ๋ณ€์ˆ˜๋“ค๋งŒ ์‚ฌ์šฉ์„ ํ–ˆ๋‹ค(maxCount,current,count)=> O(1)
36+
37+
ํ•˜์ง€๋งŒ ์ด๋ฌธ์ œ๋Š” O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ ธ๊ฐ€์•ผํ•œ๋‹ค.
38+
๊ทธ๋Ÿฌ๋ ค๋ฉด sort๋„ ํ•˜์ง€๋ง์•„์•ผํ•˜๊ณ , 2์ค‘for๋ฌธ์„ ์“ฐ์ง€๋„ ๋ง์•„์•ผํ•œ๋‹ค.
39+
(์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋‚ด๊ฐ€ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์ธ๋ฑ์Šค+1ํ•ด์„œ ํ•ด๊ฒฐํ•ด๋„ ๋˜๊ธดํ•œ๋‹ค. ๊ทธ๋ž˜๋„ ์ •๋ ฌ๋•Œ๋ฌธ์— nlogn์ด ๋œ๋‹ค.)
40+
41+
๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š”,set์„ ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
42+
*/
43+
644
/**
745
* @param {number[]} nums
846
* @return {number}
947
*/
10-
var longestConsecutive = function (nums) {
11-
const numSet = new Set(nums);
12-
let maxCount = 0;
13-
for (let i of numSet) {
14-
//n ๋ฒˆ ์ˆœํšŒ
15-
// ++ ์ด์ „์— ์—ฐ์†์ฒดํฌ๊ฐ€ ๋˜์—ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ด์ „ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด pass
16-
if (numSet.has(i - 1)) continue; //์ด๋ฏธ ์ง„ํ–‰ ๋œ ์—ฐ์†์ฒดํฌ์˜ ๊ฒฝ์šฐ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
17-
//์—ฐ์†์ด ๋˜๋Š”์ง€ ํ™•์ธํ•ด์„œ ์žˆ์œผ๋ฉด 1์ถ”๊ฐ€.
18-
let length = 0;
19-
while (numSet.has(i + length)) {
20-
//์—ฐ์†์ด ๋Š๊ธฐ๋Š” ์ˆœ๊ฐ„ ๋ฉˆ์ถ”๋Š” ๋ฐ˜๋ณต๋ฌธ. ์ฆ‰ for๋ฌธ ์ „์ฒด ํ†ตํ‹€์–ด ์ตœ๋Œ€ n๋ฒˆ ์‹คํ–‰.
21-
length++;
48+
var longestConsecutive = function(nums) {
49+
if(nums.length == 0) return 0
50+
let maxCount = 1;
51+
nums.sort((a,b)=>a-b);
52+
nums = [...new Set(nums)];
53+
let count = 1;
54+
for (let i = 0; i < nums.length - 1; i++) {
55+
if (nums[i] + 1 === nums[i + 1]) {
56+
count += 1;
57+
} else if (nums[i] === nums[i + 1]) {
58+
// ์ค‘๋ณต์ผ ๊ฒฝ์šฐ ์•„๋ฌด๊ฒƒ๋„ ์•ˆ ํ•˜๊ณ  ๋„˜์–ด๊ฐ
59+
continue;
60+
} else {
61+
maxCount = Math.max(maxCount, count);
62+
count = 1;
63+
}
2264
}
23-
maxCount = Math.max(length, maxCount);
24-
}
25-
return maxCount;
65+
maxCount = Math.max(maxCount, count); // ๋งˆ์ง€๋ง‰์—๋„ ๋น„๊ต ํ•„์š”
66+
67+
return maxCount
2668
};
69+
/*sortํ• ๋•Œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ nlog(n)์ธ๋ฐ ์™œ ํ†ต๊ณผํ–ˆ์ง€.. ๋น…์˜ค ๊ณ„์‚ฐ๋ฒ•์ด ์ตœ์•…์˜ ๊ฒฝ์šฐ๋ฅผ ๊ณ„์‚ฐํ•œ๊ฑฐ๋ผ, ์šด์ข‹๊ฒŒ ํ†ต๊ณผํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
70+
์ข€ ๋” ์ตœ์ ํ™” ํ•ด๋ณด์ž
71+
*/
2772

28-
//์‹œ๊ฐ„๋ณต์žก๋„ O(n) + O(n) = O(n) /๊ณต๊ฐ„๋ณต์žก๋„ O(n)
73+
/**
74+
* @param {number[]} nums
75+
* @return {number}
76+
*/
77+
var longestConsecutive = function(nums) {
78+
const numsSet = new Set(nums);
79+
let maxCount = 0;
80+
for(let num of numsSet){
81+
//์ค‘๋ณต๋œ ๊ณ„์‚ฐ ํŒจ์Šค
82+
if(numsSet.has(num-1)) continue;
83+
let length = 1;
84+
while(numsSet.has(num+length)){
85+
length++
86+
}
87+
maxCount = Math.max(maxCount,length)
88+
}
89+
return maxCount
90+
};
2991

30-
//์ƒ๊ฐํ•  ์ง€์ . ์–‘์ชฝ์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค๋ฉด, ์‹œ๊ฐ„๋ณต์žก๋„ ์ตœ์ ํ™” ๊ฐ€๋Šฅ
92+
/*
93+
set์„ ์ด์šฉํ•ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐ
94+
set์˜ has๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์กฐํšŒ ์ตœ์ ํ™”
95+
while์„ ์ผ์ง€๋งŒ, for๋ฌธ์˜ ์ฒซ๋ฒˆ์งธ if๋ฌธ์„ ํ†ตํ•ด ์‹ค์ œ ์ˆ˜ํ–‰์€ O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง.
96+
์ด ์™ธ์—๋„, set์œผ๋กœ ๋งŒ๋“  ๋‹ค์Œ์— ์‚ญ์ œํ•ด๊ฐ€๋ฉด์„œ(while) ์ˆœํšŒ๊ฐ€ ์•„๋‹Œ, ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ๊ฐ’๋“ค์„ ์—ฐ์†์ ์œผ๋กœ(while) ์ง€์šฐ๊ณ , ์—ฐ์†์ด ๋๋‚˜๋ฉด pop์—์„œ ๋‚˜์˜จ ๊ฐ’์˜ left right๋ฅผ ์กฐํšŒํ•ด์„œ ์—†์œผ๋ฉด ๋‹ค์‹œ ์ตœ๋Œ€๊ฐ’๊ณผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ๋‹ค.
97+
๊ทผ๋ฐ ์œ„์˜ ์ฝ”๋“œ์—์„œ has๋กœ ์กฐํšŒํ•˜๋‚˜, remove๋ฅผ ํ•˜๋ ค๋ฉด ์–ด์ฐจํ”ผ ๋˜ ์กฐํšŒํ•ด์•ผํ•˜๋‹ˆ,
98+
์„ฑ๋Šฅ์ƒ์˜ ์ฐจ์ด๋Š” ํฌ๊ฒŒ ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
99+
*/

0 commit comments

Comments
ย (0)