piles数组表示每堆石头的个数,可以进行k次操作,使石头总数最小。
每次操作可以移除floor(piles[i]/2)个石头,piles[i]可重复移除。
floor是向下取整。
思路:
greedy
每次选最大的石头堆,就能移除最多的石头数。
所以用到max heap。
public int minStoneSum(int[] piles, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a,b)->(b-a));
int res = 0;
for(int stones : piles) {
maxHeap.add(stones);
}
while(k > 0) {
int cur = maxHeap.poll();
cur -= (int)Math.floor(cur/2);
maxHeap.add(cur);
k --;
}
for(Integer stones : maxHeap) {
res += stones;
}
return res;
}