정답 소스는 아래와 같다!
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
long answer = 0;
Queue<Integer> q = new PriorityQueue<>(Comparator.reverseOrder());
for (int work : works) {
q.add(work);
}
while (n > 0 && q.peek() != 0) {
q.add(q.poll() - 1);
n--;
}
while (!q.isEmpty() && q.peek() != 0) {
int num = q.poll();
answer += (Math.pow(num, 2));
}
return answer;
}
}
아래 예시는 지독한 숏코딩을 한 번 노려본 코드이다.
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
long answer = 0;
Queue<Integer> q = new PriorityQueue<>(Comparator.reverseOrder());
for (int work : works) q.add(work);
while (n-- > 0 && q.peek() != 0) q.add(q.poll() - 1);
while (!q.isEmpty() && q.peek() != 0) answer += (Math.pow(q.poll(), 2));
return answer;
}
}