Skip to content

Commit 3e1b8cb

Browse files
committed
Time: 96 ms (16.29%), Space: 16.2 MB (79.75%) - LeetHub
1 parent c8e25e9 commit 3e1b8cb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
4+
// dp[i] = max dist you can go using i stops
5+
int n = stations.size();
6+
vector<long long int> dp(n+1,0);
7+
dp[0] = startFuel;
8+
9+
// for each station limit
10+
for(int i = 0; i < n; i++){
11+
for(int t = i; t >= 0; t--){
12+
if(dp[t] >= stations[i][0]){
13+
dp[t+1] = max(dp[t+1], dp[t] + stations[i][1]);
14+
}
15+
}
16+
}
17+
18+
for(int i = 0; i <= n; i++){
19+
if(dp[i] >= target)
20+
return i;
21+
}
22+
23+
return -1;
24+
}
25+
};

0 commit comments

Comments
 (0)