Skip to content

Commit d0c6382

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 6e5f6c5 + 6bb898f commit d0c6382

File tree

15 files changed

+519
-17
lines changed

15 files changed

+519
-17
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
return new Set(nums).size !== nums.length;
7+
};
8+
9+
//console.log(containsDuplicate([1, 2, 3, 1])); // true
10+
// console.log(containsDuplicate([1, 2, 3, 4])); // false
11+
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
contains-duplicate
3+
์š”๊ตฌ์‚ฌํ•ญ: ์ฃผ์–ด์ง„ ์ž…๋ ฅ์—์„œ ์ค‘๋ณต๋œ ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ฐธ/๊ฑฐ์ง“์œผ๋กœ ๋ฐ˜ํ™˜.
4+
์ ‘๊ทผ 1: ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค๋ฅธ vector input_list๋ฅผ ๋‘๊ณ , ์ž…๋ ฅ ๋ฐ˜๋ณต๋งˆ๋‹ค ์ „์ฒด ์Šค์บ”ํ•ด์„œ ์ค‘๋ณต ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
5+
input_list์˜ ์Šค์บ” ๋น„์šฉ O(N)์„ ๋ชจ๋“  ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^2)์ž…๋‹ˆ๋‹ค.
6+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” ์ž…๋ ฅ๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ O(N)์ž…๋‹ˆ๋‹ค.
7+
์ ‘๊ทผ 2: ์›์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ์ฒดํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
8+
set์€ ์ค‘๋ณต ์›์†Œ๋ฅผ ๊ฒ€์ถœํ•˜๋Š”๋ฐ ํšจ๊ณผ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ๋‚ด๋ถ€ ๊ตฌํ˜„์— ๋”ฐ๋ผ ๋น„์šฉ๊ณผ ํŠน์„ฑ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
9+
- C++์—์„œ ordered_{set, map}์€ ์ •๋ ฌ์„ ์œ ์ง€ํ•˜๋Š” search tree๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ฟผ๋ฆฌ์™€ ์‚ฝ์ž…, ์‚ญ์ œ ๋น„์šฉ์ด ํ‰๊ท  O(log(N))์ž…๋‹ˆ๋‹ค.
10+
search tree์˜ ๊ตฌํ˜„์— ๋”ฐ๋ผ ์ตœ์•…์˜ ๊ฒฝ์šฐ ์ฟผ๋ฆฌ ๋น„์šฉ์ด O(N)๊นŒ์ง€ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
11+
- C++์—์„œ unordered_{set, map}์€ ํ•ด์‹œ ๋ฐ ๋ฒ„ํ‚ท ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ํ‰๊ท ์ ์ธ ์ฟผ๋ฆฌ, ์‚ฝ์ž…, ์‚ญ์ œ ๋น„์šฉ์€ O(1)์ž…๋‹ˆ๋‹ค.
12+
์šฐ๋ฆฌ์˜ ์š”๊ตฌ์‚ฌํ•ญ์€ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ณ„๋„์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ unordered_set์œผ๋กœ ์ค‘๋ณต ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋น„์šฉ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.
13+
์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž…๋ ฅ์„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. N๊ฐœ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ฟผ๋ฆฌ์™€ ์ž…๋ ฅ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ, ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N), ๊ณต๊ฐ„ ๋ณต์žก๋„๋„ ์ž…๋ ฅ๋งŒํผ์ธ O(N)์ž…๋‹ˆ๋‹ค.
14+
*/
15+
16+
#include <unordered_set>
17+
#include <vector>
18+
19+
class Solution {
20+
public:
21+
bool containsDuplicate(std::vector<int>& nums) {
22+
std::unordered_set<int> s;
23+
24+
for (auto it = nums.begin(); it != nums.end(); it++) {
25+
int x = *it;
26+
if (s.find(x) != s.end())
27+
return true;
28+
else
29+
s.insert(x);
30+
}
31+
return false;
32+
}
33+
};

โ€Žcontains-duplicate/grapefruitgreentealoe.jsโ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@ var containsDuplicate = function (nums) {
2424
}
2525
return false;
2626
};
27+
28+
//25.7.23 ํ’€์ด์‹œ๊ฐ„ 10๋ถ„
29+
/**
30+
* @param {number[]} nums
31+
* @return {boolean}
32+
*/
33+
var containsDuplicate = function(nums) {
34+
//๋‘๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚˜๋Š”๊ฒŒ ์žˆ์œผ๋ฉด ๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜์‹œ์˜ค.
35+
//์ด์ค‘ ์ˆœํšŒํ•˜๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผํ•  ์˜ˆ์ •.
36+
//์‹œ๊ฐ„๋ณต์žก๋„ ์ค‘์š”.
37+
//1. ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๊ทธ ๋ฐฐ์—ด์— ๊ฐ’์ด ์žˆ์œผ๋ฉด ๋ฆฌํ„ด false.
38+
//2.๋ฐฐ์—ด๋ณด๋‹ค Set์„ ์‚ฌ์šฉํ•œ,์‚ฝ์ž… ๋ฐ ์กฐํšŒ ์†๋„ ์ตœ์ ํ™” ํ™œ์šฉํ•˜๊ธฐ.
39+
const container = new Set();
40+
for(let i =0; i<nums.length;i++){
41+
if(container.has(nums[i])) return true;
42+
else container.add(nums[i])
43+
}
44+
return false
45+
};
46+
//์œ„์˜ ์ฝ”๋“œ๋ณด๋‹ค, ๋ฆฌํ„ด์„ ๋” ๋นจ๋ฆฌํ•œ๋‹ค๋Š” ์ด์ ์ด ์žˆ๋‹ค.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
let answer = false
3+
for(let i = 0; i< nums.length; i ++ ){
4+
if(nums.findIndex((value)=> value === nums[i]) !== i){
5+
answer = true
6+
break;
7+
}
8+
}
9+
return answer
10+
};
11+
//-> time limit ์ดˆ๊ณผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
12+
//์‹œ๊ฐ„๋ณต์žก๋„
13+
// for + findIndex O(nยฒ) โŒ
14+
// for + Set or Map O(n) โœ…
15+
//์ค‘๋ณต ์—ฌ๋ถ€๋งŒ ์ฒดํฌํ•  ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด Set์„ ์“ฐ๋Š” ๊ฒŒ ํšจ์œจ์ 
16+
// includes, findIndex, indexOf๋Š” ์ ˆ๋Œ€ ๋ฃจํ”„ ์•ˆ์—์„œ ์“ฐ์ง€ ๋ง๊ฒƒ โ€” O(nยฒ)
17+
function containsDuplicate(nums: number[]): boolean {
18+
const seen = new Set(); //์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ์ˆซ์ž๋“ค์„ ์ €์žฅ
19+
for (const num of nums) {
20+
if (seen.has(num)) return true; // ์ค‘๋ณต ๋ฐœ๊ฒฌ
21+
seen.add(num); // ์ค‘๋ณต์ด ์•„๋‹ ๊ฒฝ์šฐ ์ถ”๊ฐ€
22+
}
23+
return false; // ์ค‘๋ณต ์—†์Œ
24+
}
25+
//Set : ์ค‘๋ณต ์—†๋Š” ๊ฐ’์„์˜ ๋ชจ์Œ. ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ƒ๊ฒผ์ง€๋งŒ, ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ (์ค‘๋ณต์ œ๊ฑฐ)
26+
//Map : key โ†’ value ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด (๊ฒ€์ƒ‰ ์ˆ˜์ •)
27+

โ€Žhouse-robber/grapefruitgreentealoe.jsโ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,23 @@ var rob2 = function (nums) {
4343
};
4444
//๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ O(1)๋กœ ๊ฐœ์„ 
4545

46+
47+
//7.21 ํ’€์ด์‹œ๊ฐ„ 10๋ถ„ ์†Œ์š”
48+
49+
50+
/**
51+
* @param {number[]} nums
52+
* @return {number}
53+
*/
54+
var rob = function(nums) {
55+
const dp = new Array(nums.length+1).fill(0);
56+
dp[1] = nums[0]
57+
for(let i = 2; i<=nums.length;i++){
58+
dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i-1])
59+
}
60+
return dp[nums.length]
61+
};
62+
/**
63+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
64+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
65+
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function(nums) {
6+
if(nums.length === 0) return 0;
7+
8+
const numSet = new Set(nums);
9+
let longest = 0;
10+
11+
for (const num of numSet) {
12+
if(!numSet.has(num-1)) {
13+
let currentNum = num;
14+
let currentLength = 1;
15+
16+
while(numSet.has(currentNum+1)) {
17+
currentNum++;
18+
currentLength++;
19+
}
20+
21+
longest = Math.max(longest, currentLength)
22+
}
23+
}
24+
return longest;
25+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
longest-consecutive-sequence
3+
์š”๊ตฌ์‚ฌํ•ญ: ์ฃผ์–ด์ง„ ๋น„์ •๋ ฌ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ๊ธด ์—ฐ์† ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ O(N)์— ๊ตฌํ•˜๋ผ.
4+
์ ‘๊ทผ 1: ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋ฐฉ๋ฒ•์€ ์ •๋ ฌ ํ›„ ์Šค์บ”ํ•˜์—ฌ ๊ฐ€์žฅ ๊ธด ์—ฐ์† ๊ตฌ๊ฐ„์„ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค.
5+
์ •๋ ฌ ๋น„์šฉ O(NlogN)์— ์Šค์บ” O(N)์œผ๋กœ ์ด O(NlogN)์ด๋‹ค. ๋น„์šฉ ์ดˆ๊ณผ.
6+
์ ‘๊ทผ 2: ์ •๋ ฌํ•˜์ง€ ์•Š๊ณ , ์ธ์ ‘ํ•œ ์—ฐ์† ์›์†Œ ์ˆ˜์—ด์„ 'ํ•˜๋‚˜์˜ ๋ฉ์–ด๋ฆฌ'๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ์œ„ํ•ด union-findํ•  ์ˆ˜ ์žˆ๋‹ค.
7+
์ค‘๋ณต ์›์†Œ๋Š” ๋ฌด์‹œํ•œ๋‹ค.
8+
์ž…๋ ฅ x์— ๋Œ€ํ•ด x-1์˜ ์œ ๋‹ˆ์˜จ์„ ์ฐพ๋Š”๋‹ค.
9+
๊ทธ๋Ÿฌํ•œ ์œ ๋‹ˆ์˜จ์ด ์กด์žฌํ•˜๋ฉด x๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ทธ ์ดํ›„ x+1 ์œ ๋‹ˆ์˜จ์ด ์กด์žฌํ•˜๋ฉด ํ†ตํ•ฉํ•œ๋‹ค.
10+
๊ทธ๋Ÿฌํ•œ ์œ ๋‹ˆ์˜จ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด x+1 ์œ ๋‹ˆ์˜จ์„ ์ฐพ๋Š”๋‹ค.
11+
๊ทธ๋Ÿฌํ•œ ์œ ๋‹ˆ์˜จ์ด ์กด์žฌํ•˜๋ฉด x๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
12+
์ธ์ ‘ํ•œ ์•ž ๋’ค ์œ ๋‹ˆ์˜จ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ž…๋ ฅ์„ ์ƒˆ๋กœ์šด root union์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
13+
14+
๋น„์šฉ:
15+
๊ฒฝ๋กœ ์••์ถ•๊ณผ ๋†’์ด ์ตœ์†Œํ™” ์ตœ์ ํ™”๋ฅผ ์ ์šฉํ•˜๋ฉด, find ๋น„์šฉ์€ O(ฮฑ(N))์œผ๋กœ ์ตœ๋Œ€ ์ž…๋ ฅ N=10^5์— ๋Œ€ํ•ด์„œ ํ•œ ์ž๋ฆฌ ์ƒ์ˆ˜๋งŒํผ ์ถฉ๋ถ„ํžˆ ๋‚ฎ๋‹ค.
16+
์œ ๋‹ˆ์˜จ์˜ ํ†ตํ•ฉ ๋น„์šฉ์€ O(1)์ด๋‹ค.
17+
N๊ฐœ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด O(1) ์—ฐ์‚ฐ์„ ์ƒ์ˆ˜๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์œ ๋‹ˆ์˜จ ๋นŒ๋”ฉ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.
18+
์‚ฌ์ด์ฆˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์Šค์บ”ํ•˜์—ฌ ๊ฐ€์žฅ ํฐ ์œ ๋‹ˆ์˜จ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•œ๋‹ค. ์ด๋Š” O(K) where K <= N์ด๋‹ค.
19+
๋”ฐ๋ผ์„œ ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.
20+
๊ณต๊ฐ„ ๋ณต์žก๋„๋„ ์ž…๋ ฅ ๋ฒ”์œ„์˜ ์ƒ์ˆ˜๋ฐฐ์ด๋ฏ€๋กœ O(N)์ด๋‹ค.
21+
*/
22+
23+
#include <unordered_map>
24+
#include <vector>
25+
26+
#define max(a, b) ((a) > (b) ? (a) : (b))
27+
28+
class Solution {
29+
public:
30+
std::unordered_map<int, int> parent;
31+
std::unordered_map<int, int> size;
32+
33+
int find(int x) {
34+
if (x == parent[x]) return x;
35+
return parent[x] = find(parent[x]); // path compression
36+
}
37+
void union_sets(int x, int y) {
38+
int px = find(x);
39+
int py = find(y);
40+
41+
if (px == py) return;
42+
43+
// min height optimization
44+
if (size[px] > size[py]) {
45+
size[px] += size[py];
46+
parent[py] = px;
47+
} else {
48+
size[py] += size[px];
49+
parent[px] = py;
50+
}
51+
}
52+
int longestConsecutive(std::vector<int>& nums) {
53+
// build union
54+
for (auto it = nums.begin(); it != nums.end(); it++) {
55+
int x = *it;
56+
57+
if (parent.find(x) != parent.end())
58+
continue;
59+
60+
if (parent.find(x - 1) != parent.end()) {
61+
parent[x] = find(x - 1);
62+
size[parent[x]]++;
63+
if (parent.find(x + 1) != parent.end()) {
64+
union_sets(x, x + 1);
65+
}
66+
} else if (parent.find(x + 1) != parent.end()) {
67+
parent[x] = find(x + 1);
68+
size[parent[x]]++;
69+
} else {
70+
parent[x] = x;
71+
size[x] = 1;
72+
}
73+
}
74+
75+
// find largest set
76+
int max_size = 0;
77+
for(auto it = size.begin(); it != size.end(); it++) {
78+
auto sz = it->second;
79+
max_size = max(max_size, sz);
80+
}
81+
return max_size;
82+
}
83+
};
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+
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var topKFrequent = function(nums, k) {
7+
//HashMap ์„ ์–ธ
8+
const counter = new Map()
9+
10+
//HashMap์— ๋นˆ๋„๋ฅผ value๋กœ ์ €์žฅ
11+
for(const num of nums){
12+
counter.set(num,(counter.get(num)|| 0) +1);
13+
}
14+
15+
//keys๋ฅผ ๊ฐ€์ ธ์™€ ์ •๋ ฌ ํ›„, k๋งŒํผ -slice ๋ฆฌํ„ด
16+
return [...counter.keys()]
17+
.sort((a,b) => counter.get(a) - counter.get(b))
18+
.slice(-k)
19+
};

0 commit comments

Comments
ย (0)