안녕하세요.! 오늘은 프로그래머스의 야근 지수 문제를 풀어보겠습니다.😊
https://programmers.co.kr/learn/courses/30/lessons/12927
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
long sum = 0;
PriorityQueue<Integer> pQueue = new PriorityQueue(Collections.reverseOrder());
for(int num : works) {
pQueue.offer(num);
}
for(int i=0; i<n; i++) {
int max = pQueue.poll();
if(max <= 0) break;
pQueue.offer(max-1);
}
while(!pQueue.isEmpty()) {
sum += Math.pow(pQueue.poll(), 2);
}
return sum;
}
}
초기 착안을 잘못했지 않았나 싶다. 수학적으로 접근해야하는 줄 알고 나누고 더하고 엄청 했는데 알고보니 우선순위큐 이용해서 가장 큰 값을 가진 큐에서 하나하나씩 빼주면 되는거였다.. 풀듯 말듯 안풀려서 이틀을 고민했는데 결국 못풀고 답을 봐서 아쉬움이 남는다. 어쨌든 우선순위 큐의 성질에 대해서 다시 한 번 짚고 넘어갈 수 있었던 좋은 문제였다.