function solution(no, works) {
let answer = 0
while(no > 0) {
const index = works.findIndex(el => el === Math.max(...works))
if(works[index] <= 0) break; <= 테스트 케이스 7, 8
works[index] -= 1
no -= 1
}
for(let i in works) {
answer = answer + (works[i] * works[i])
}
return answer
}
while 내 if 문은 추후에 추가한 코드이다. if 문 없이 채점을 돌려봤더니 테스트 케이스 7, 8번이 통과되지 않았었다. 이유를 생각해보니, 모든 요소가 0이 되고도 작업 가능한 시간이 남아있다면, 계속해서 작업량을 차감시키게 되어 작업량은 음수가 된다. 음수가 된 작업량을 제곱하면 양수가 되기 때문에 배상 비용을 최소화 시키지 못하게 된다. 따라서 작업량들의 최대값이 0이라면(즉 모든 작업량이 0이 되었다면) 반복을 중지시켜야 한다.
class MaxHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
while (parentIndex !== 0 && this.heap[parentIndex] < value) {
const temp = this.heap[parentIndex];
this.heap[parentIndex] = value;
this.heap[currentIndex] = temp;
currentIndex = parentIndex;
parentIndex = Math.floor(currentIndex / 2);
}
}
pop() {
if (this.heap.length === 2) return this.heap.pop(); // 루트 정점만 남은 경우
const returnValue = this.heap[1];
this.heap[1] = this.heap.pop();
let currentIndex = 1;
let leftIndex = 2;
let rightIndex = 3;
while (this.heap[currentIndex] < this.heap[leftIndex] ||
this.heap[currentIndex] < this.heap[rightIndex]) {
if (this.heap[leftIndex] < this.heap[rightIndex]) {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[rightIndex];
this.heap[rightIndex] = temp;
currentIndex = rightIndex;
} else {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[leftIndex];
this.heap[leftIndex] = temp;
currentIndex = leftIndex;
}
leftIndex = currentIndex * 2;
rightIndex = currentIndex * 2 + 1;
}
return returnValue;
}
}
function solution(no, works) {
// 모든 작업량의 합이 작업 가능한 시간보다 작거나 같으면,
// 배상가능한 비용은 무조건 0이 되어 계산할 필요가 없음
if(works.reduce((a, b) => a + b) <= no) return 0
// 최대 힙 생성
const heap = new MaxHeap()
// 최대 힙에 주어진 works의 값들 삽입
for(let work in works) {
heap.push(works[work])
}
// no 만큼 for loop을 돌면서 작업량 1씩 차감
for(let i = 0; i < no; i++) {
heap.push(heap.pop() - 1)
}
return heap.heap.reduce((a, b) => a + b * b)
}