Skip to content

Commit 6b3c6dc

Browse files
committed
πŸ’‘ ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 42628 - μ΄μ€‘μš°μ„ μˆœμœ„ν
1 parent e99840e commit 6b3c6dc

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
⭐️ 문제 정보 ⭐️
3+
문제 : 42628 - μ΄μ€‘μš°μ„ μˆœμœ„ν
4+
레벨 : Level 3
5+
링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42628
6+
*/
7+
8+
// ANCHOR : 26.02.05 풀이
9+
class Heap {
10+
constructor(compare) {
11+
this.heap = [];
12+
this.compare = compare;
13+
}
14+
15+
size() {
16+
return this.heap.length;
17+
}
18+
19+
push(value) {
20+
this.heap.push(value);
21+
this.siftUp(this.heap.length - 1);
22+
}
23+
24+
pop() {
25+
if (this.heap.length === 0) return null;
26+
const root = this.heap[0];
27+
if (this.heap.length === 1) return this.heap.pop();
28+
this.heap[0] = this.heap.pop();
29+
this.siftDown(0);
30+
31+
return root;
32+
}
33+
34+
siftUp(i) {
35+
while (i > 0) {
36+
const p = Math.floor((i - 1) / 2);
37+
if (this.compare(this.heap[i], this.heap[p])) {
38+
this.swap(i, p);
39+
i = p;
40+
} else break;
41+
}
42+
}
43+
44+
siftDown(i) {
45+
if (this.heap.length === 0) return;
46+
47+
while (true) {
48+
const lc = 2 * i + 1 >= this.heap.length ? null : 2 * i + 1;
49+
const rc = 2 * i + 2 >= this.heap.length ? null : 2 * i + 2;
50+
51+
let c;
52+
if (lc === null) break;
53+
else if (rc === null) c = lc;
54+
else {
55+
this.compare(this.heap[lc], this.heap[rc]) ? (c = lc) : (c = rc);
56+
}
57+
58+
if (this.compare(this.heap[c], this.heap[i])) {
59+
this.swap(c, i);
60+
i = c;
61+
} else break;
62+
}
63+
}
64+
65+
swap(a, b) {
66+
[this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];
67+
}
68+
}
69+
70+
function solution(operations) {
71+
const minHeap = new Heap((a, b) => a.value < b.value);
72+
const maxHeap = new Heap((a, b) => a.value > b.value);
73+
74+
let id = 0;
75+
const deleted = new Set();
76+
for (const operation of operations) {
77+
const [op, value] = operation.split(" ");
78+
if (op === "I") {
79+
const num = Number(value);
80+
minHeap.push({ id, value: num });
81+
maxHeap.push({ id, value: num });
82+
id++;
83+
} else if (op === "D") {
84+
if (value === "1") {
85+
// MAX heapμ—μ„œ μ‚­μ œ
86+
while (maxHeap.size() > 0) {
87+
const x = maxHeap.pop();
88+
if (deleted.has(x.id)) continue;
89+
else {
90+
deleted.add(x.id);
91+
break;
92+
}
93+
}
94+
} else if (value === "-1") {
95+
// MIN heapμ—μ„œ μ‚­μ œ
96+
while (minHeap.size() > 0) {
97+
const x = minHeap.pop();
98+
if (deleted.has(x.id)) continue;
99+
else {
100+
deleted.add(x.id);
101+
break;
102+
}
103+
}
104+
}
105+
}
106+
}
107+
108+
function popValid(heap, deleted) {
109+
while (heap.size() > 0) {
110+
const node = heap.pop();
111+
if (node === null) return null;
112+
if (deleted.has(node.id)) continue;
113+
return node;
114+
}
115+
return null;
116+
}
117+
118+
const maxNode = popValid(maxHeap, deleted);
119+
if (maxNode === null) return [0, 0];
120+
const minNode = popValid(minHeap, deleted);
121+
if (minNode === null) return [0, 0];
122+
123+
return [maxNode.value, minNode.value];
124+
}

β€ŽProgrammers/README.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
| 42583 | 닀리λ₯Ό μ§€λ‚˜λŠ” 트럭 | [42583_닀리λ₯Ό_μ§€λ‚˜λŠ”_트럭.js](Level2/42583_닀리λ₯Ό_μ§€λ‚˜λŠ”_트럭.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42583) |
3939
| 42584 | 주식가격 | [42584_주식가격.js](Level2/42584_주식가격.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42584) |
4040
| 42586 | κΈ°λŠ₯개발 | [42586_κΈ°λŠ₯개발.js](Level2/42586_κΈ°λŠ₯개발.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42586) |
41+
| 42628 | μ΄μ€‘μš°μ„ μˆœμœ„ν | [42628_μ΄μ€‘μš°μ„ μˆœμœ„ν.js](Level3/42628_μ΄μ€‘μš°μ„ μˆœμœ„ν.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42628) |
4142
| 42840 | λͺ¨μ˜κ³ μ‚¬ | [42840_λͺ¨μ˜κ³ μ‚¬.js](Level1/42840_λͺ¨μ˜κ³ μ‚¬.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42840) |
4243
| 42888 | μ˜€ν”ˆμ±„νŒ…λ°© | [42888_μ˜€ν”ˆμ±„νŒ…λ°©.js](Level2/42888_μ˜€ν”ˆμ±„νŒ…λ°©.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42888) |
4344
| 42889 | μ‹€νŒ¨μœ¨ | [42889_μ‹€νŒ¨μœ¨.js](Level1/42889_μ‹€νŒ¨μœ¨.js) | [πŸ”—](https://school.programmers.co.kr/learn/courses/30/lessons/42889) |

0 commit comments

Comments
Β (0)