-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1962.cpp
More file actions
35 lines (33 loc) · 819 Bytes
/
1962.cpp
File metadata and controls
35 lines (33 loc) · 819 Bytes
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
class Solution {
public:
int minStoneSum(vector<int>& piles, int k) {
int sum = accumulate(piles.begin(), piles.end(), 0);
priority_queue<int> pq(piles.begin(), piles.end());
while (k--) {
int t = pq.top();
pq.pop();
sum -= t / 2;
pq.push(t - t / 2);
}
return sum;
}
};
class Solution {
public:
int minStoneSum(vector<int>& piles, int k) {
priority_queue<int> pq;
int sum = 0;
for (auto& pile : piles) {
sum += pile;
pq.push(pile);
}
while (k--) {
int maxOne = pq.top();
pq.pop();
int remove = maxOne / 2;
sum -= remove;
pq.push(maxOne - remove);
}
return sum;
}
};