1962. 移除石子使总数最小

1962. 移除石子使总数最小

解法一: 优先队列

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int minStoneSum(int[] piles, int k) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((num1, num2) -> num2 - num1);
for (int pile : piles) {
priorityQueue.offer(pile);
}
for (int i = 0; i < k; i++) {
priorityQueue.offer((priorityQueue.poll() + 1) >> 1);
}
int sum = 0;
for (Integer num : priorityQueue) {
sum += num;
}
return sum;
}
}
作者

wuhunyu

发布于

2023-12-23

更新于

2023-12-23

许可协议