forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
71 lines (56 loc) · 2.11 KB
/
main2.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/// Source : https://leetcode.com/problems/minimum-number-of-refueling-stops/description/
/// Author : liuyubobobo
/// Time : 2018-08-05
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(n^3)
/// Space Complexity: O(n)
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
if(startFuel >= target)
return 0;
int n = stations.size();
vector<vector<int>> dp(2, vector<int>(n, 0));
for(int i = n - 1; i >= 0; i --)
if(startFuel >= stations[i][0]){
dp[1][i] = startFuel + stations[i][1];
if(dp[1][i] >= target)
return 1;
}
for(int k = 2; k <= n; k ++){
for(int last = k - 2; last < n; last ++)
for(int cur = last + 1; cur < n ; cur ++)
if(dp[(k-1)&1][last] >= stations[cur][0]){
dp[k&1][cur] = max(dp[k&1][cur], dp[(k-1)&1][last] + stations[cur][1]);
if(dp[k&1][cur] >= target)
return k;
}
else
break;
}
return -1;
}
};
int main() {
int target1 = 1, startFuel1 = 1;
vector<vector<int>> stations1;
cout << Solution().minRefuelStops(target1, startFuel1, stations1) << endl;
// 0
int target2 = 100, startFuel2 = 1;
vector<vector<int>> stations2 = {{10, 100}};
cout << Solution().minRefuelStops(target2, startFuel2, stations2) << endl;
// -1
int target3 = 100, startFuel3 = 10;
vector<vector<int>> stations3 = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};
cout << Solution().minRefuelStops(target3, startFuel3, stations3) << endl;
// 2
int target4 = 1000, startFuel4 = 83;
vector<vector<int>> stations4 = {{25, 27}, {36, 187}, {140, 186}, {378, 6}, {492, 202},
{517, 89}, {579, 234}, {673, 86}, {808, 53}, {954, 49}};
cout << Solution().minRefuelStops(target4, startFuel4, stations4) << endl;
// -1
return 0;
}