java: add «2558. Take Gifts From the Richest Pile»
URL: https://leetcode.com/problems/take-gifts-from-the-richest-pile/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a58d78d2eb
commit
da959699dd
1 changed files with 19 additions and 0 deletions
19
java/take-gifts-from-the-richest-pile.java
Normal file
19
java/take-gifts-from-the-richest-pile.java
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue