max heap
Time: O(nlogn) + O(klogn)
Space: O(n)
public class Solution {
public static int chocolatesRemained(int[] chocolates,int iterations){
PriorityQueue<Integer> q = new PriorityQueue<>((a,b)->(b-a));
for(int i=0;i<chocolates.length;i++){
q.offer(chocolates[i]);
}
while(iterations > 0){
int curr = q.poll();
curr = (int) Math.sqrt(curr);
q.offer(curr);
iterations--;
}
int remainedChocolates = 0;
while(!q.isEmpty()){
remainedChocolates+=q.poll();
}
return remainedChocolates;
}
public static void main(String[] args) {
int[] chocolates = {25, 64, 9, 4, 100};
int iterations = 4;
System.out.println(chocolatesRemained(chocolates,iterations));//29
}
}