-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminRefuelStops.cpp
More file actions
50 lines (48 loc) · 1.49 KB
/
minRefuelStops.cpp
File metadata and controls
50 lines (48 loc) · 1.49 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
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
int minRefuelStops(int t, int start, vector<vector<int>>& ss) {
priority_queue<int> pq;
int n = ss.size(), idx = 0;
int remain = start, loc = 0, ans = 0;
while (loc < t) {
if (remain == 0) {
if (!pq.empty() && ++ans >= 0) {
remain += pq.top();
pq.pop();
}
else return -1;
}
loc += remain;
remain = 0;
while (idx < n && ss[idx][0] <= loc)
pq.push(ss[idx++][1]);
}
return ans;
}
};
// DP
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;
}
};
// 作者:LeetCode-Solution
// 链接:https://leetcode.cn/problems/minimum-number-of-refueling-stops/solution/zui-di-jia-you-ci-shu-by-leetcode-soluti-nmga/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。