Matej Focko
da959699dd
URL: https://leetcode.com/problems/take-gifts-from-the-richest-pile/ Signed-off-by: Matej Focko <me@mfocko.xyz>
19 lines
413 B
Java
19 lines
413 B
Java
class Solution {
|
|
public long pickGifts(int[] gifts, int k) {
|
|
var q = new PriorityQueue<Integer>(Comparator.reverseOrder());
|
|
for (var gift : gifts) {
|
|
q.offer(gift);
|
|
}
|
|
|
|
for (int i = 0; i < k; ++i) {
|
|
int richest = q.poll();
|
|
q.offer((int) Math.sqrt(richest));
|
|
}
|
|
|
|
long remaining = 0;
|
|
while (!q.isEmpty()) {
|
|
remaining += q.poll();
|
|
}
|
|
return remaining;
|
|
}
|
|
}
|