⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions Programmers/Level3/42628_이중우선순위큐.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
⭐️ 문제 정보 ⭐️
문제 : 42628 - 이중우선순위큐
레벨 : Level 3
링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42628
*/

// ANCHOR : 26.02.05 풀이
class Heap {
constructor(compare) {
this.heap = [];
this.compare = compare;
}

size() {
return this.heap.length;
}

push(value) {
this.heap.push(value);
this.siftUp(this.heap.length - 1);
}

pop() {
if (this.heap.length === 0) return null;
const root = this.heap[0];
if (this.heap.length === 1) return this.heap.pop();
this.heap[0] = this.heap.pop();
this.siftDown(0);

return root;
}

siftUp(i) {
while (i > 0) {
const p = Math.floor((i - 1) / 2);
if (this.compare(this.heap[i], this.heap[p])) {
this.swap(i, p);
i = p;
} else break;
}
}

siftDown(i) {
if (this.heap.length === 0) return;

while (true) {
const lc = 2 * i + 1 >= this.heap.length ? null : 2 * i + 1;
const rc = 2 * i + 2 >= this.heap.length ? null : 2 * i + 2;

let c;
if (lc === null) break;
else if (rc === null) c = lc;
else {
this.compare(this.heap[lc], this.heap[rc]) ? (c = lc) : (c = rc);
}

if (this.compare(this.heap[c], this.heap[i])) {
this.swap(c, i);
i = c;
} else break;
}
}

swap(a, b) {
[this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];
}
}

function solution(operations) {
const minHeap = new Heap((a, b) => a.value < b.value);
const maxHeap = new Heap((a, b) => a.value > b.value);

let id = 0;
const deleted = new Set();
for (const operation of operations) {
const [op, value] = operation.split(" ");
if (op === "I") {
const num = Number(value);
minHeap.push({ id, value: num });
maxHeap.push({ id, value: num });
id++;
} else if (op === "D") {
if (value === "1") {
// MAX heap에서 삭제
while (maxHeap.size() > 0) {
const x = maxHeap.pop();
if (deleted.has(x.id)) continue;
else {
deleted.add(x.id);
break;
}
}
} else if (value === "-1") {
// MIN heap에서 삭제
while (minHeap.size() > 0) {
const x = minHeap.pop();
if (deleted.has(x.id)) continue;
else {
deleted.add(x.id);
break;
}
}
}
}
}

function popValid(heap, deleted) {
while (heap.size() > 0) {
const node = heap.pop();
if (node === null) return null;
if (deleted.has(node.id)) continue;
return node;
}
return null;
}

const maxNode = popValid(maxHeap, deleted);
if (maxNode === null) return [0, 0];
const minNode = popValid(minHeap, deleted);
if (minNode === null) return [0, 0];

return [maxNode.value, minNode.value];
}
1 change: 1 addition & 0 deletions Programmers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
| 42583 | 다리를 지나는 트럭 | [42583_다리를_지나는_트럭.js](Level2/42583_다리를_지나는_트럭.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42583) |
| 42584 | 주식가격 | [42584_주식가격.js](Level2/42584_주식가격.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42584) |
| 42586 | 기능개발 | [42586_기능개발.js](Level2/42586_기능개발.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42586) |
| 42628 | 이중우선순위큐 | [42628_이중우선순위큐.js](Level3/42628_이중우선순위큐.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42628) |
| 42840 | 모의고사 | [42840_모의고사.js](Level1/42840_모의고사.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42840) |
| 42888 | 오픈채팅방 | [42888_오픈채팅방.js](Level2/42888_오픈채팅방.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42888) |
| 42889 | 실패율 | [42889_실패율.js](Level1/42889_실패율.js) | [🔗](https://school.programmers.co.kr/learn/courses/30/lessons/42889) |
Expand Down