871. Minimum Number of Refueling Stops
https://leetcode.com/problems/minimum-number-of-refueling-stops/
/*
* @lc app=leetcode id=871 lang=cpp
*
* [871] Minimum Number of Refueling Stops
*/
// @lc code=start
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
const int N = stations.size();
vector<long> dp(N + 1, startFuel);
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j >= 1; --j) {
if (dp[j - 1] >= stations[i][0]) {
dp[j] = max(dp[j], dp[j - 1] + stations[i][1]);
}
}
}
for (int i = 0; i <= N; ++i) {
if (dp[i] >= target) return i;
}
return -1;
}
};
// @lc code=endLast updated