1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public long pickGifts(int[] gifts, int k) { Queue<Integer> maxHeap = new PriorityQueue<>((o1, o2) -> Integer.compare(o2, o1)); for (int gift : gifts) { maxHeap.offer(gift); }
for (int i = 0; i < k; i++) { maxHeap.offer((int) Math.sqrt(maxHeap.poll())); }
long result = 0; while (!maxHeap.isEmpty()) { result += maxHeap.poll(); } return result; } }
|
References
2558. Take Gifts From the Richest Pile