[Algorithm] μ΄μ€μ°μ μμ ν
π λ¬Έμ
μ΄μ€ μ°μ μμ νλ λ€μ μ°μ°μ ν μ μλ μλ£κ΅¬μ‘°λ₯Ό λ§ν©λλ€.
λͺ λ Ήμ΄μμ ν(λμ΄)I μ«μ | νμ μ£Όμ΄μ§ μ«μλ₯Ό μ½μ ν©λλ€. |
D 1 | νμμ μ΅λκ°μ μμ ν©λλ€. |
D -1 | νμμ μ΅μκ°μ μμ ν©λλ€. |
μ΄μ€ μ°μ μμ νκ° ν μ°μ° operationsκ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, λͺ¨λ μ°μ°μ μ²λ¦¬ν ν νκ° λΉμ΄μμΌλ©΄ [0,0] λΉμ΄μμ§ μμΌλ©΄ [μ΅λκ°, μ΅μκ°]μ return νλλ‘ solution ν¨μλ₯Ό ꡬνν΄μ£ΌμΈμ.
μ ν μ¬ν
- operationsλ κΈΈμ΄κ° 1 μ΄μ 1,000,000 μ΄νμΈ λ¬Έμμ΄ λ°°μ΄μ λλ€.
- operationsμ μμλ νκ° μνν μ°μ°μ λνλ
λλ€.
- μμλ “λͺ λ Ήμ΄ λ°μ΄ν°” νμμΌλ‘ μ£Όμ΄μ§λλ€.- μ΅λκ°/μ΅μκ°μ μμ νλ μ°μ°μμ μ΅λκ°/μ΅μκ°μ΄ λ μ΄μμΈ κ²½μ°, νλλ§ μμ ν©λλ€.
- λΉ νμ λ°μ΄ν°λ₯Ό μμ νλΌλ μ°μ°μ΄ μ£Όμ΄μ§ κ²½μ°, ν΄λΉ μ°μ°μ 무μν©λλ€.
μ μΆλ ₯ μ
["I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"] | [0,0] |
["I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333"] | [333, -45] |
βοΈ νμ΄
λ°°μ΄(operations)μ κΈΈμ΄κ° 1,000,000 μ΄νμ΄κΈ° λλ¬Έμ λΉμ°ν λ§€λ² μ λ ¬μ μμΌμ£Όλ κ²μ ν¨μ¨μ± μΈ‘λ©΄μμ ν΅κ³Όλμ§ μμ κ²μ΄λΌκ³ μκ°νλ€. μ΄ν νμ μ¬μ©ν΄μ νλ λ°©λ²μ μλνλ €κ³ κ³ λ―Όνλλ° μ΅μκ°, μ΅λκ° λ κ°μ§λ₯Ό κ³ λ €ν΄μΌ νκΈ° λλ¬Έμ max heapκ³Ό min heapμ λ λ€ μ¬μ©ν΄μΌκ² λ€κ³ μκ°νκ³ λ¬Έμ λ₯Ό νκΈ° μμνλ€.
"I" λͺ λ Ήμ΄κ° μ λ ₯λλ©΄ λ κ°μ§ νμ κ°μ λ£μ΄μ€λ€. "D" λͺ λ Ήμ΄κ° μ λ ₯λλ©΄ μ΅λκ°μ λΉΌλ 1μ κ²½μ° max heapμμ μ΅λκ°μ λΉΌμ£Όκ³ λ°λμΈ κ²½μ°μλ min heapμμ μ΅μκ°μ 빼쀬λ€. μΆκ°μ μΌλ‘ cntλΌλ λ³μλ₯Ό ν΅ν΄μ νμ¬ νμ μλ μ«μ κ°μλ₯Ό μΉ΄μ΄ν ν΄μ€¬λλ° λ£μ΄μ€ λλ λλ€ λ£μ΄μ£Όμ§λ§ λΊλλ κ°κ°μ νμμ λΉΌκΈ° λλ¬Έμ κ°―μ μ°¨μ΄κ° λκΈ° λλ¬Έμ΄λ€. forλ¬Έμ λκ³ answerλ₯Ό λ°ννκΈ° μ μ answerμ κ°κ° νμμ λΊ κ°μ λ£μ΄μ£Όλλ° μ΄λ μλμΉ μμ κ°μ΄ λμ¬ μ μλ€. λ§μ½ heapμ "I"λͺ λ Ήμ΄κ° 10λ², "D 1" λͺ λ Ήμ΄κ° 10λ² μ΄λ£¨μ΄μ§λ©΄ νμλ κ°μ΄ μμ΄ [0, 0]μ λ°νν΄μΌ νλλ° min heapμλ κ°μ΄ λ¨μμκΈ° λλ¬Έμ λ€λ₯Έ κ°μ λ°ννκ² λλ€. λ°λΌμ cntκ° 0μ΄ λλ κ²½μ°μ κ° heapμ λΉμμ£ΌκΈ° μν¨μ΄λ€.
β¨οΈ μ½λ
class MinHeap {
constructor() {
this.heap = [];
this.heap.push(-1e9);
}
insert(value) {
this.heap.push(value);
this.upheap(this.heap.length - 1);
}
upheap(pos) {
let temp = this.heap[pos];
while (temp < this.heap[parseInt(pos / 2)]) {
this.heap[pos] = this.heap[parseInt(pos / 2)];
pos = parseInt(pos / 2);
}
this.heap[pos] = temp;
}
get() {
if (this.size() === 0) return 0;
if (this.size() === 1) return this.heap.pop();
const result = this.heap[1];
this.heap[1] = this.heap.pop();
this.downheap(1, this.heap.length - 1);
return result;
}
downheap(pos, len) {
let temp = this.heap[pos],
child;
while (pos <= parseInt(len / 2)) {
child = pos * 2;
if (child < len && this.heap[child] > this.heap[child + 1]) child++;
if (temp <= this.heap[child]) break;
this.heap[pos] = this.heap[child];
pos = child;
}
this.heap[pos] = temp;
}
clear() {
this.heap = [-1e9];
}
size() {
return this.heap.length - 1;
}
}
class MaxHeap {
constructor() {
this.heap = [];
this.heap.push(1e9);
}
insert(value) {
this.heap.push(value);
this.upheap(this.heap.length - 1);
}
upheap(pos) {
let temp = this.heap[pos];
while (temp > this.heap[parseInt(pos / 2)]) {
this.heap[pos] = this.heap[parseInt(pos / 2)];
pos = parseInt(pos / 2);
}
this.heap[pos] = temp;
}
get() {
if (this.size() === 0) return 0;
if (this.size() === 1) return this.heap.pop();
const result = this.heap[1];
this.heap[1] = this.heap.pop();
this.downheap(1, this.heap.length - 1);
return result;
}
downheap(pos, len) {
let temp = this.heap[pos],
child;
while (pos <= parseInt(len / 2)) {
child = pos * 2;
if (child < len && this.heap[child] < this.heap[child + 1]) child++;
if (temp >= this.heap[child]) break;
this.heap[pos] = this.heap[child];
pos = child;
}
this.heap[pos] = temp;
}
clear() {
this.heap = [1e9];
}
size() {
return this.heap.length - 1;
}
}
function solution(operations) {
var answer = [];
let cnt = 0;
const maxHeap = new MaxHeap();
const minHeap = new MinHeap();
for (const o of operations) {
const [cmd, n] = o.split(' ');
if (cmd === 'I') {
maxHeap.insert(+n);
minHeap.insert(+n);
cnt += 1;
} else {
if (cnt) {
if (n === '1') maxHeap.get();
else minHeap.get();
cnt -= 1;
}
if (!cnt) {
minHeap.clear();
maxHeap.clear();
}
}
}
answer.push(maxHeap.get());
answer.push(minHeap.get());
return answer;
}