Skip to content

Commit 1e0fcda

Browse files
author
Eric
committed
92차 1번 문제풀이(진행중)
1 parent 8d642eb commit 1e0fcda

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

live9/test92/문제1/황장현.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
// 루트 값을 뺐을 때 재정렬
33+
bubbleDown() {
34+
let index = 0;
35+
const length = this.heap.length;
36+
37+
while (true) {
38+
const left = 2 * index + 1;
39+
const right = 2 * index + 2;
40+
41+
let min = Math.min(this.heap[left], this.heap[right]);
42+
[this.heap[index], min] = [min, this.heap[index]];
43+
let smallest = index;
44+
45+
if (left < length && this.heap[left] < this.heap[smallest]) {
46+
smallest = left;
47+
}
48+
49+
if (right < length && this.heap[right] < this.heap[smallest]) {
50+
smallest = right;
51+
}
52+
53+
if (smallest === index) break;
54+
55+
[this.heap[index], this.heap[smallest]] = [
56+
this.heap[smallest],
57+
this.heap[index],
58+
];
59+
index = smallest;
60+
}
61+
}
62+
63+
least() {
64+
return this.heap[0];
65+
}
66+
}
67+
68+
function calculateScovile(num1, num2) {
69+
return num1 + num2 * 2;
70+
}
71+
72+
function solution(scoville, K) {
73+
scoville.sort((a, b) => a - b);
74+
if (scoville[0] >= K) return 0;
75+
76+
const minHeap = new MinHeap();
77+
scoville.forEach((s) => minHeap.push(s));
78+
}
79+
80+
console.log(solution([1, 2, 3, 9, 10, 12], 7));

0 commit comments

Comments
 (0)