[Algorithm] ์ผ๊ทผ ์ง์
๋ฌธ์
ํ์ฌ์ Demi๋ ๊ฐ๋์ ์ผ๊ทผ์ ํ๋๋ฐ์, ์ผ๊ทผ์ ํ๋ฉด ์ผ๊ทผ ํผ๋ก๋๊ฐ ์์ ๋๋ค. ์ผ๊ทผ ํผ๋ก๋๋ ์ผ๊ทผ์ ์์ํ ์์ ์์ ๋จ์ ์ผ์ ์์ ๋์ ์ ๊ณฑํ์ฌ ๋ํ ๊ฐ์ ๋๋ค. Demi๋ N์๊ฐ ๋์ ์ผ๊ทผ ํผ๋ก๋๋ฅผ ์ต์ํํ๋๋ก ์ผํ ๊ฒ๋๋ค.Demi๊ฐ 1์๊ฐ ๋์ ์์ ๋ 1๋งํผ์ ์ฒ๋ฆฌํ ์ ์๋ค๊ณ ํ ๋, ํด๊ทผ๊น์ง ๋จ์ N ์๊ฐ๊ณผ ๊ฐ ์ผ์ ๋ํ ์์ ๋ works์ ๋ํด ์ผ๊ทผ ํผ๋ก๋๋ฅผ ์ต์ํํ ๊ฐ์ ๋ฆฌํดํ๋ ํจ์ solution์ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- works๋ ๊ธธ์ด 1 ์ด์, 20,000 ์ดํ์ธ ๋ฐฐ์ด์ ๋๋ค.
- works์ ์์๋ 50000 ์ดํ์ธ ์์ฐ์์ ๋๋ค.
- n์ 1,000,000 ์ดํ์ธ ์์ฐ์์ ๋๋ค.
์ ์ถ๋ ฅ ์
[4, 3, 3] | 4 | 12 |
[2, 1, 2] | 1 | 6 |
[1,1] | 3 | 0 |
ํ์ด
์ฒ์ ์๋ํ๋ ์ ๊ทผ ๋ฐฉํฅ์ ํ๊ท ๊ฐ์ ๋ด๋ฉฐ ๊ทธ๋ฆฌ๋์ฒ๋ผ ํธ๋ ๋ฐฉ๋ฒ์ด์๋ค. ๋จ์ ์์ ๋์ ์ ๊ณฑ๊ฐ์ผ๋ก ํผ๋ก๋๊ฐ ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์ ๊ฐ ๋จ์ ์์ ๋์ ์ฐจ์ด๊ฐ ์๋งํ๊ฒ ๋์ผํ๋ค๊ณ ์๊ฐํ๋ค. ๋จผ์ ๋ชจ๋ ์์ ๋์์ n์ ๋นผ์ค ๋ค์ for๋ฌธ์ผ๋ก ๋๋ฉด์ ํ๊ท ๊ฐ์ ๊ตฌํ๊ณ ์ด๋ฅผ ๋ฐ๋ณต๋ฌธ์ ํ์ฌ ๊ฐ๊ณผ ๋น๊ตํด์ answer์ ๋ํด์ฃผ๋ ๋ฐฉ๋ฒ์ผ๋ก ์๋ํ๋ค. ์์ ๋ ๋ง์์ง๋ง ์ ์ถ์ ํ๊ณ ๋ณด๋ ๋๋ถ๋ถ์ ์ผ์ด์ค์์ ์คํจ๋ฅผ ํ๋๋ฐ ์ด์ ๋ฅผ ์๊ฐํด๋ณด๋ ํ์ฌ๊ฐ์ด ๊ต์ฅํ ํฌ๊ณ ๋ค์ ๊ฐ๋ค์ด ๋๋ถ๋ถ ์์ ๊ฒฝ์ฐ ํ์ฌ ๊ฐ์์ ๋ง์ด ๋นผ์ค์ผํ์ง๋ง ํ๊ท ๊ฐ์ ์ํฅ์ ๋ฐ์ ์๊ตฌํ๋ ๊ฐ๋งํผ์ ์ฒ๋ฆฌ๋ฅผ ๋ชปํ๋ ๊ฒ์ ์๊ฒ ๋์๋ค. ์ด๋ฐ ์๋ ์ดํ ๊ทธ๋ ๋ค๋ฉด ๊ฐ์ฅ ํฐ ๊ฐ์์๋ถํฐ ํ๋์ฉ ๋นผ์ค์ผ๊ฒ ๋ค๋ผ๊ณ ์๊ฐํ๋ค.
๋ ๋ฒ์งธ ์๋๋ก๋ while๋ฌธ์ผ๋ก n์ด 0 ์ด๊ณผ์ผ๋ ๋์๊ฐ๋๋ก ํ๊ณ ๋จ์ ์์ ๋๋ค์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ ์ ์ผ ์์ ์๋ ๊ฐ์์ ๋นผ์ฃผ๋ ๋ฐฉ๋ฒ์ด์๋ค. ํ์ง๋ง ๋งค๋ฒ sort ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค๋ฉด ์๊ฐ์ด๊ณผ๋ก ์ด์ด์ง ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๊ณ ๊ตฌํํ์๋ max heap์ ์ฌ์ฉํ๋ค. while๋ฌธ์ด ์ข ๋ฃ๋๋ฉด ๋ ์ด์ ๋บ ๊ฐ์ด ์๊ฑฐ๋ ๋นผ์ง ์์๋ ๋๋ ์ํฉ์ด๋ฏ๋ก heap์ ์กด์ฌํ๋ ๊ฐ์ answer์ ์ ๊ณฑํด์ ๋ฃ์ด์ค์ผ๋ก์ ํต๊ณผํ ์ ์์๋ค.
์ฝ๋
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 false;
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;
}
size() {
return this.heap.length - 1;
}
}
function solution(n, works) {
var answer = 0;
const len = works.length;
const heap = new MaxHeap();
for (const work of works) heap.insert(work);
while (n > 0) {
let num = heap.get();
if (!num) return 0;
heap.insert(num - 1);
n -= 1;
}
for (let i = 1; i < heap.size() + 1; i++) answer += heap.heap[i] ** 2;
return answer;
}