Skip to content

Commit 1aabc2d

Browse files
committed
Feat: 5주차 문제
1 parent 09af8cd commit 1aabc2d

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed

Diff for: 5주차/박종운/골드4__카드_정렬하기.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const local_input = `
2+
3
3+
10
4+
20
5+
40
6+
`;
7+
8+
const input = process.execArgv.includes("--stack-size=65536")
9+
? require("fs").readFileSync("dev/stdin").toString()
10+
: local_input;
11+
12+
const inputs = input.trim().split("\n");
13+
const arr = inputs.map(Number);
14+
15+
class MinHeap {
16+
constructor() {
17+
this.heap = [];
18+
}
19+
20+
push(value) {
21+
this.heap.push(value);
22+
let idx = this.heap.length - 1;
23+
24+
while (idx > 0 && this.heap[idx] < this.heap[this.getParent(idx)]) {
25+
this.swap(idx, this.getParent(idx));
26+
idx = this.getParent(idx);
27+
}
28+
}
29+
30+
getParent(index) {
31+
return Math.floor((index - 1) / 2);
32+
}
33+
34+
getLeft(index) {
35+
return index * 2 + 1;
36+
}
37+
38+
getRight(index) {
39+
return index * 2 + 2;
40+
}
41+
42+
swap(index1, index2) {
43+
const temp = this.heap[index1];
44+
this.heap[index1] = this.heap[index2];
45+
this.heap[index2] = temp;
46+
}
47+
48+
pop() {
49+
if (!this.heap.length) {
50+
return null;
51+
}
52+
53+
const min = this.heap[0];
54+
const last = this.heap.pop();
55+
56+
if (this.heap.length) {
57+
this.heap[0] = last;
58+
59+
let idx = 0;
60+
61+
while (this.getLeft(idx) < this.heap.length) {
62+
const left = this.getLeft(idx);
63+
const right = this.getRight(idx);
64+
65+
let minChild = left;
66+
67+
if (right < this.heap.length && this.heap[right] < this.heap[left]) {
68+
minChild = right;
69+
}
70+
71+
if (this.heap[idx] < this.heap[minChild]) {
72+
break;
73+
}
74+
75+
this.swap(idx, minChild);
76+
idx = minChild;
77+
}
78+
}
79+
80+
return min;
81+
}
82+
}
83+
84+
const minHeap = new MinHeap();
85+
let sum = 0;
86+
87+
arr.slice(1).map(el => {
88+
minHeap.push(el);
89+
})
90+
91+
while(minHeap.heap.length > 1) {
92+
const min1 = minHeap.pop();
93+
const min2 = minHeap.pop();
94+
sum += min2 + min1;
95+
minHeap.push(min2 + min1);
96+
97+
console.log(min1, min2, sum, minHeap.heap);
98+
}
99+
100+
console.log(sum)

Diff for: 5주차/박종운/레벨2__더_맵게.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
6+
push(value) {
7+
this.heap.push(value);
8+
let idx = this.heap.length - 1;
9+
10+
while (idx > 0 && this.heap[idx] < this.heap[this.getParent(idx)]) {
11+
this.swap(idx, this.getParent(idx));
12+
idx = this.getParent(idx);
13+
}
14+
}
15+
16+
getParent(index) {
17+
return Math.floor((index - 1) / 2);
18+
}
19+
20+
getLeft(index) {
21+
return index * 2 + 1;
22+
}
23+
24+
getRight(index) {
25+
return index * 2 + 2;
26+
}
27+
28+
swap(index1, index2) {
29+
const temp = this.heap[index1];
30+
this.heap[index1] = this.heap[index2];
31+
this.heap[index2] = temp;
32+
}
33+
34+
pop() {
35+
if (!this.heap.length) {
36+
return null;
37+
}
38+
39+
const min = this.heap[0];
40+
const last = this.heap.pop();
41+
42+
if (this.heap.length) {
43+
this.heap[0] = last;
44+
45+
let idx = 0;
46+
47+
while (this.getLeft(idx) < this.heap.length) {
48+
const left = this.getLeft(idx);
49+
const right = this.getRight(idx);
50+
51+
let minChild = left;
52+
53+
if (right < this.heap.length && this.heap[right] < this.heap[left]) {
54+
minChild = right;
55+
}
56+
57+
if (this.heap[idx] < this.heap[minChild]) {
58+
break;
59+
}
60+
61+
this.swap(idx, minChild);
62+
idx = minChild;
63+
}
64+
}
65+
66+
return min;
67+
}
68+
}
69+
70+
function solution(scoville, K) {
71+
let answer = 0;
72+
73+
const minHeap = new MinHeap();
74+
75+
scoville.forEach((el) => minHeap.push(el));
76+
77+
while (minHeap.heap.length > 1 && minHeap.heap[0] < K) {
78+
const min1 = minHeap.pop();
79+
const min2 = minHeap.pop();
80+
const newVal = min1 + min2 * 2;
81+
minHeap.push(newVal);
82+
answer++;
83+
}
84+
85+
return (minHeap.heap[0] >= K) ? answer : -1;
86+
}

Diff for: 5주차/박종운/실버1__최소_힙.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const local_input = `
2+
9
3+
0
4+
12345678
5+
1
6+
2
7+
0
8+
0
9+
0
10+
0
11+
32
12+
`;
13+
14+
const input = process.execArgv.includes("--stack-size=65536")
15+
? require("fs").readFileSync("dev/stdin").toString()
16+
: local_input;
17+
18+
const inputs = input.trim().split("\n");
19+
const arr = inputs.map(Number);
20+
21+
class MinHeap {
22+
constructor() {
23+
this.heap = [];
24+
}
25+
26+
push(value) {
27+
this.heap.push(value);
28+
let idx = this.heap.length - 1;
29+
30+
while (idx > 0 && this.heap[idx] < this.heap[this.getParent(idx)]) {
31+
this.swap(idx, this.getParent(idx));
32+
idx = this.getParent(idx);
33+
}
34+
}
35+
36+
getParent(index) {
37+
return Math.floor((index - 1) / 2);
38+
}
39+
40+
getLeft(index) {
41+
return index * 2 + 1;
42+
}
43+
44+
getRight(index) {
45+
return index * 2 + 2;
46+
}
47+
48+
swap(index1, index2) {
49+
const temp = this.heap[index1];
50+
this.heap[index1] = this.heap[index2];
51+
this.heap[index2] = temp;
52+
}
53+
54+
pop() {
55+
if (!this.heap.length) {
56+
return null;
57+
}
58+
59+
const min = this.heap[0];
60+
const last = this.heap.pop();
61+
62+
if (this.heap.length) {
63+
this.heap[0] = last;
64+
65+
let idx = 0;
66+
67+
while (this.getLeft(idx) < this.heap.length) {
68+
const left = this.getLeft(idx);
69+
const right = this.getRight(idx);
70+
71+
let minChild = left;
72+
73+
if (right < this.heap.length && this.heap[right] < this.heap[left]) {
74+
minChild = right;
75+
}
76+
77+
if (this.heap[idx] < this.heap[minChild]) {
78+
break;
79+
}
80+
81+
this.swap(idx, minChild);
82+
idx = minChild;
83+
}
84+
}
85+
86+
return min;
87+
}
88+
}
89+
90+
const minHeap = new MinHeap();
91+
const result = [];
92+
93+
arr.slice(1).map(el => {
94+
if(el != 0) {
95+
minHeap.push(el);
96+
} else {
97+
const min = minHeap.pop();
98+
result.push(min ?? 0);
99+
}
100+
})
101+
102+
console.log(result.join('\n'))

0 commit comments

Comments
 (0)