1.1046. 最后一块石头的重量
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> q;
for (int s: stones) {
q.push(s);
}
while (q.size() > 1) {
int a = q.top();
q.pop();
int b = q.top();
q.pop();
if (a > b) {
q.push(a - b);
}
}
return q.empty() ? 0 : q.top();
}
};
2.6112. 装满杯子需要的最短总时长
class Solution {
public:
int fillCups(vector<int>& amount) {
sort(amount.begin(), amount.end());
if (amount[0] + amount[1] <= amount[2]) return amount[2];
else {
int t = amount[0] + amount[1] - amount[2];
return (t + 1) / 2 + amount[2];
}
}
};
3.1753. 移除石子的最大得分
class Solution:
def maximumScore(self, a: int, b: int, c: int) -> int:
a,b,c = sorted([a,b,c])
if a+b <= c:
return (a+b)
else:
return (a+b+c)
4.871. 最低加油次数
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
int n = stations.size();
vector<long> dp(n + 1);
dp[0] = startFuel;
for (int i = 0; i < n; i++) {
for (int j = i; j >= 0; j--) {
if (dp[j] >= stations[i][0]) {
dp[j + 1] = max(dp[j + 1], dp[j] + stations[i][1]);
}
}
}
for (int i = 0; i <= n; i++) {
if (dp[i] >= target) {
return i;
}
}
return -1;
}
};