forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
64 lines (48 loc) · 1.73 KB
/
main3.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
/// 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^2)
/// 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<int> dp(n + 1, 0);
dp[0] = startFuel;
for(int i = 0 ; i < n ; i ++)
for(int t = i ; t >= 0 ; t --)
if(dp[t] >= stations[i][0]){
dp[t + 1] = max(dp[t + 1], dp[t] + stations[i][1]);
}
for(int t = 0; t <= n; t ++)
if(dp[t] >= target)
return t;
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;
}