Skip to content

Commit f71b15a

Browse files
Merge pull request #734 from eric-hjh/main
[황장현] 118차 라이브 코테 제출
2 parents 821633f + 456f987 commit f71b15a

2 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
push(value) {
6+
this.heap.push(value);
7+
this.bubbleUp();
8+
}
9+
10+
pop() {
11+
if (this.heap.length === 1) return this.heap.pop();
12+
const min = this.heap[0];
13+
this.heap[0] = this.heap.pop();
14+
this.bubbleDown();
15+
return min;
16+
}
17+
18+
bubbleUp() {
19+
let index = this.heap.length - 1;
20+
21+
while (index > 0) {
22+
const parentIndex = Math.floor((index - 1) / 2);
23+
if (this.heap[parentIndex] <= this.heap[index]) break;
24+
[this.heap[parentIndex], this.heap[index]] = [
25+
this.heap[index],
26+
this.heap[parentIndex],
27+
];
28+
index = parentIndex;
29+
}
30+
}
31+
32+
bubbleDown() {
33+
let index = 0;
34+
const length = this.heap.length;
35+
36+
while (true) {
37+
const left = 2 * index + 1;
38+
const right = 2 * index + 2;
39+
let smallest = index;
40+
41+
if (left < length && this.heap[left] < this.heap[smallest]) {
42+
smallest = left;
43+
}
44+
45+
if (right < length && this.heap[right] < this.heap[smallest]) {
46+
smallest = right;
47+
}
48+
49+
if (smallest === index) break;
50+
51+
[this.heap[index], this.heap[smallest]] = [
52+
this.heap[smallest],
53+
this.heap[index],
54+
];
55+
56+
index = smallest;
57+
}
58+
}
59+
60+
least() {
61+
return this.heap[0];
62+
}
63+
64+
size() {
65+
return this.heap.length;
66+
}
67+
}
68+
69+
const input = require('fs')
70+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
71+
.toString()
72+
.trim()
73+
.split('\n');
74+
75+
function solution(input) {
76+
const T = input[0];
77+
let result = [];
78+
let lineIndex = 1;
79+
80+
for (let t = 0; t < T; t++) {
81+
const K = parseInt(input[lineIndex++]);
82+
const files = input[lineIndex++].split(' ').map(Number);
83+
const minHeap = new MinHeap();
84+
for (let i = 0; i < K; i++) {
85+
minHeap.push(files[i]);
86+
}
87+
88+
let sum = 0;
89+
while (minHeap.size() > 1) {
90+
const a = minHeap.pop();
91+
const b = minHeap.pop();
92+
sum += a + b;
93+
minHeap.push(a + b);
94+
}
95+
96+
result.push(sum);
97+
}
98+
99+
return result.join('\n');
100+
}
101+
102+
console.log(solution(input));
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
push(value) {
6+
this.heap.push(value);
7+
this.bubbleUp();
8+
}
9+
10+
pop() {
11+
if (this.heap.length === 1) return this.heap.pop();
12+
const min = this.heap[0];
13+
this.heap[0] = this.heap.pop();
14+
this.bubbleDown();
15+
return min;
16+
}
17+
18+
bubbleUp() {
19+
let index = this.heap.length - 1;
20+
21+
while (index > 0) {
22+
const parentIndex = Math.floor((index - 1) / 2);
23+
if (this.heap[parentIndex] <= this.heap[index]) break;
24+
[this.heap[parentIndex], this.heap[index]] = [
25+
this.heap[index],
26+
this.heap[parentIndex],
27+
];
28+
index = parentIndex;
29+
}
30+
}
31+
32+
bubbleDown() {
33+
let index = 0;
34+
const length = this.heap.length;
35+
36+
while (true) {
37+
const left = 2 * index + 1;
38+
const right = 2 * index + 2;
39+
let smallest = index;
40+
41+
if (left < length && this.heap[left] < this.heap[smallest]) {
42+
smallest = left;
43+
}
44+
45+
if (right < length && this.heap[right] < this.heap[smallest]) {
46+
smallest = right;
47+
}
48+
49+
if (smallest === index) break;
50+
51+
[this.heap[index], this.heap[smallest]] = [
52+
this.heap[smallest],
53+
this.heap[index],
54+
];
55+
56+
index = smallest;
57+
}
58+
}
59+
60+
least() {
61+
return this.heap[0];
62+
}
63+
64+
size() {
65+
return this.heap.length;
66+
}
67+
}
68+
69+
const input = require('fs')
70+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
71+
.toString()
72+
.trim()
73+
.split('\n')
74+
.map(Number);
75+
76+
function solution(input) {
77+
const N = input[0];
78+
const cards = input.slice(1).map(Number);
79+
const minHeap = new MinHeap();
80+
for (let i = 0; i < N; i++) {
81+
minHeap.push(cards[i]);
82+
}
83+
84+
let sum = 0;
85+
while (minHeap.size() > 1) {
86+
const a = minHeap.pop();
87+
const b = minHeap.pop();
88+
sum += a + b;
89+
minHeap.push(a + b);
90+
}
91+
return sum;
92+
}
93+
94+
console.log(solution(input));

0 commit comments

Comments
 (0)