π λ¬Έμ
xx νμ¬μ 2xNλͺ μ μ¬μλ€μ Nλͺ μ© λ νμΌλ‘ λλ μ«μ κ²μμ νλ €κ³ ν©λλ€. λ κ°μ νμ κ°κ° Aνκ³Ό Bνμ΄λΌκ³ νκ² μ΅λλ€. μ«μ κ²μμ κ·μΉμ λ€μκ³Ό κ°μ΅λλ€.
- λ¨Όμ λͺ¨λ μ¬μμ΄ λ¬΄μμλ‘ μμ°μλ₯Ό νλμ© λΆμ¬λ°μ΅λλ€.
- κ° μ¬μμ λ± ν λ²μ© κ²½κΈ°λ₯Ό ν©λλ€.
- κ° κ²½κΈ°λΉ Aνμμ ν μ¬μμ΄, Bνμμ ν μ¬μμ΄ λμ μλ‘μ μλ₯Ό 곡κ°ν©λλ€. κ·Έλ μ«μκ° ν° μͺ½μ΄ μΉλ¦¬νκ² λκ³ , μΉλ¦¬ν μ¬μμ΄ μν νμ μΉμ μ 1μ μ»κ² λ©λλ€.
- λ§μ½ μ«μκ° κ°λ€λ©΄ λꡬλ μΉμ μ μ»μ§ μμ΅λλ€.
μ 체 μ¬μλ€μ μ°μ 무μμλ‘ μμ°μλ₯Ό νλμ© λΆμ¬λ°μμ΅λλ€. κ·Έλ€μ Aνμ λΉ λ₯΄κ² μΆμ μμλ₯Ό μ νκ³ μμ λ€μ μΆμ μμλ₯Ό Bνμκ² κ³΅κ°ν΄λ²λ Έμ΅λλ€. Bνμ κ·Έκ²μ λ³΄κ³ μμ λ€μ μ΅μ’
μΉμ μ κ°μ₯ λμ΄λ λ°©λ²μΌλ‘ νμλ€μ μΆμ μμλ₯Ό μ νμ΅λλ€. μ΄λμ Bνμ΄ μ»λ μΉμ μ ꡬν΄μ£ΌμΈμ.
A νμλ€μ΄ λΆμ¬λ°μ μκ° μΆμ μμλλ‘ λμ΄λμ΄μλ λ°°μ΄ Aμ iλ²μ§Έ μμκ° Bνμ iλ² νμμ΄ λΆμ¬λ°μ μλ₯Ό μλ―Ένλ λ°°μ΄ Bκ° μ£Όμ΄μ§ λ, B νμλ€μ΄ μ»μ μ μλ μ΅λ μΉμ μ return νλλ‘ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
μ ν μ¬ν
- Aμ Bμ κΈΈμ΄λ κ°μ΅λλ€.
- Aμ Bμ κΈΈμ΄λ 1 μ΄μ 100,000 μ΄νμ λλ€.
- Aμ Bμ κ° μμλ 1 μ΄μ 1,000,000,000 μ΄νμ μμ°μμ λλ€.
μ μΆλ ₯ μ
[5,1,3,7] | [2,2,6,8] | 3 |
[2,2,2,2] | [1,1,1,1] | 0 |
βοΈ νμ΄
λͺ¨λ μν©μ λΉκ΅νκΈ°μλ κ° λ°°μ΄μ κΈΈμ΄κ° ν¬κΈ° λλ¬Έμ μ λ λΆκ°λ₯ν κ²μ΄λΌκ³ μκ°νλ€. Bνμ΄ λ§μ΄ μ΄κΈ°κΈ° μν΄μλ Aνμ΄ ν° μμΌ λ μ λ μ΄κΈ°μ§ λͺ»νλ κ²½μ°μ μ μΌ μμ μλ₯Ό μ΄κΈΈ μ μλ κ²½μ°μ μμ μλ‘ μ΄κΈΈ μ μλ€λ©΄ μμ μλ₯Ό ν° μλ‘ μ΄κ²¨μΌ νλ€λ©΄ ν°μλ₯Ό λ΄λ κ²μ΄ ν° μλ₯Ό μλΌλ©° μ΅μ μ λ°©ν₯μΌλ‘ κ°λ κ²μ΄λΌκ³ μκ°νλ€.
Aνμ μμλ μκ΄μμ΄ λ§μ΄ μ΄κΈ°λ κ²½μ°λ₯Ό μ°Ύλ κ²μ΄κΈ° λλ¬Έμ λ¨Όμ Aνμ ν°μλλ‘ μ λ ¬ν΄μ£Όκ³ λΉκ΅ν΄μΌ νλ€. μκ° μμ¬μλ κ²½μ° μμ μΈμ΄ μ λ΅μ μ¬μ©ν μ μκΈ° λλ¬Έμ΄λ€. κ·Έλ¦¬κ³ κ°μ₯ μμ μμ ν° μλ₯Ό κ΄λ¦¬νκΈ° μν΄μ max heap, min heapμ μ¬μ©νλ€. μμμ μΈμ΄ μ λ΅λλ‘ μ λ ¬λ Aμ μμμ λΉκ΅νλ€.
1. λ§μ½ κ°μ₯ μμ μλ‘ μ΄κΈΈ μ μλ κ²½μ°μλ min heapμμ κ°μ λΉΌκ³ μ΄κΈ°λ κ²½μ°μ΄λ―λ‘ answerλ₯Ό μ¦κ°μν¨λ€.
2. κ°μ₯ ν° μλ‘ μ΄κΈΈ μ μλ κ²½μ° min heapμμ μ μΌ μμ κ°μ λΉΌκ³ μ§λ κ²½μ°μ΄κΈ° λλ¬Έμ answerλ₯Ό μ¦κ°μν€μ§ μλλ€.
3. μμ 쑰건μ ν΅κ³Όνκ³ ν° μλ‘ μ΄κΈΈ μ μλ κ²½μ°μλ max heapμμ κ°μ λΉΌκ³ answerλ₯Ό μ¦κ°μν¨λ€.
β¨οΈ μ½λ
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;
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;
}
show() {
if (this.size() === 0) return;
return this.heap[1];
}
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;
}
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;
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;
}
show() {
if (this.size() === 0) return;
return this.heap[1];
}
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;
}
size() {
return this.heap.length - 1;
}
}
function solution(A, B) {
var answer = 0;
const maxHeap = new MaxHeap();
const minHeap = new MinHeap();
A.sort((a, b) => b - a);
for (const b of B) {
maxHeap.insert(b);
minHeap.insert(b);
}
for (const a of A) {
if (a < minHeap.show()) {
minHeap.get();
answer += 1;
} else if (a >= maxHeap.show()) {
minHeap.get();
} else {
maxHeap.get();
answer += 1;
}
}
return answer;
}
'Algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Algorithm] μ΅κ³ μ μ§ν© (0) | 2023.03.01 |
---|---|
[Algorithm] λ² μ€νΈ μ¨λ² (0) | 2023.02.28 |
[Algorithm] λ¨μμΉ΄λ©λΌ (0) | 2023.02.26 |
[Algorithm] μ΄μ€μ°μ μμ ν (0) | 2023.02.25 |
[Algorithm] μΌκ·Ό μ§μ (0) | 2023.02.24 |