From 6b3c6dc55f5c44fa016f6e98e2848bb9b0ecf264 Mon Sep 17 00:00:00 2001 From: yoouyeon Date: Thu, 5 Feb 2026 22:11:06 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A1=20=ED=94=84=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EB=9E=98=EB=A8=B8=EC=8A=A4=2042628=20-=20=EC=9D=B4=EC=A4=91?= =?UTF-8?q?=EC=9A=B0=EC=84=A0=EC=88=9C=EC=9C=84=ED=81=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\354\210\234\354\234\204\355\201\220.js" | 124 ++++++++++++++++++ Programmers/README.md | 1 + 2 files changed, 125 insertions(+) create mode 100644 "Programmers/Level3/42628_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.js" diff --git "a/Programmers/Level3/42628_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.js" "b/Programmers/Level3/42628_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.js" new file mode 100644 index 0000000..503125e --- /dev/null +++ "b/Programmers/Level3/42628_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.js" @@ -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]; +} diff --git a/Programmers/README.md b/Programmers/README.md index 4869032..5c7180a 100644 --- a/Programmers/README.md +++ b/Programmers/README.md @@ -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) |