题目
解题思路
- 每次对当前石子最多的堆进行操作;
- 利用优先队列的倒序排列,可以便捷的获取最大的石子堆;
- 统计操作前的石子总数;
- 获取优先队列的首节点,移除对应石子,再将移除后的石子堆添加回队列,同时总数减去移除的石子。
代码展示
class Solution {
public int minStoneSum(int[] piles, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
int sum = 0;
for (int pile : piles){
pq.add(pile);
sum += pile;
}
while (--k >= 0){
int pile = pq.poll();
int temp = pile / 2;
sum -= temp;
pq.add(pile - temp);
}
return sum;
}
}