Priority queue를 이용한 문제
어제와 문제가 거의 유사하다.
입력 받은 배열에 있는 숫자를 큐에 넣고 루트 값을 계산 해준 후 다시 큐에 넣는다.
합산 해준 값을 리턴했다.
class Solution {
public long pickGifts(int[] gifts, int k) {
PriorityQueue<Integer> pq = new PriorityQueue(Collections.reverseOrder());
for (int i = 0; i < gifts.length; i++) {
pq.offer(gifts[i]);
}
for (int i = 0; i < k; i++) {
pq.add((int) (Math.sqrt(pq.poll())));
}
return pq.stream().mapToLong(Integer::longValue).sum();
}
}