-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1962-remove-stones-to-minimize-the-total.js
More file actions
39 lines (35 loc) · 1.11 KB
/
1962-remove-stones-to-minimize-the-total.js
File metadata and controls
39 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 1962. Remove Stones to Minimize the Total
* https://leetcode.com/problems/remove-stones-to-minimize-the-total/
* Difficulty: Medium
*
* You are given a 0-indexed integer array piles, where piles[i] represents the number
* of stones in the ith pile, and an integer k. You should apply the following operation
* exactly k times:
* - Choose any piles[i] and remove ceil(piles[i] / 2) stones from it.
*
* Notice that you can apply the operation on the same pile more than once.
*
* Return the minimum possible total number of stones remaining after applying the k operations.
*
* ceil(x) is the smallest integer that is greater than or equal to x (i.e., rounds x up).
*/
/**
* @param {number[]} piles
* @param {number} k
* @return {number}
*/
var minStoneSum = function(piles, k) {
const maxHeap = new PriorityQueue((a, b) => b - a);
let result = 0;
for (const pile of piles) {
maxHeap.enqueue(pile);
result += pile;
}
while (k-- > 0) {
const largest = maxHeap.dequeue();
maxHeap.enqueue(largest - Math.floor(largest / 2));
result -= Math.floor(largest / 2);
}
return result;
};