Minimum Number of Refueling Stops Solutions in C++
Number 871
Difficulty Hard
Acceptance 31.5%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// 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^2)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(n + 1, 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][last] >= stations[cur][0]){dp[k][cur] = max(dp[k][cur], dp[k-1][last] + stations[cur][1]);if(dp[k][cur] >= target)return k;}elsebreak;}return -1;}};int main() {int target1 = 1, startFuel1 = 1;vector<vector<int>> stations1;cout << Solution().minRefuelStops(target1, startFuel1, stations1) << endl;// 0int target2 = 100, startFuel2 = 1;vector<vector<int>> stations2 = {{10, 100}};cout << Solution().minRefuelStops(target2, startFuel2, stations2) << endl;// -1int target3 = 100, startFuel3 = 10;vector<vector<int>> stations3 = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};cout << Solution().minRefuelStops(target3, startFuel3, stations3) << endl;// 2int 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;// -1return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// 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;}elsebreak;}return -1;}};int main() {int target1 = 1, startFuel1 = 1;vector<vector<int>> stations1;cout << Solution().minRefuelStops(target1, startFuel1, stations1) << endl;// 0int target2 = 100, startFuel2 = 1;vector<vector<int>> stations2 = {{10, 100}};cout << Solution().minRefuelStops(target2, startFuel2, stations2) << endl;// -1int target3 = 100, startFuel3 = 10;vector<vector<int>> stations3 = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};cout << Solution().minRefuelStops(target3, startFuel3, stations3) << endl;// 2int 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;// -1return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// 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<long long> 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;// 0int target2 = 100, startFuel2 = 1;vector<vector<int>> stations2 = {{10, 100}};cout << Solution().minRefuelStops(target2, startFuel2, stations2) << endl;// -1int target3 = 100, startFuel3 = 10;vector<vector<int>> stations3 = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};cout << Solution().minRefuelStops(target3, startFuel3, stations3) << endl;// 2int 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;// -1return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-number-of-refueling-stops/description//// Author : liuyubobobo/// Time : 2020-05-10#include <iostream>#include <vector>#include <queue>using namespace std;/// Greedy using PQ/// Time Complexity: O(nlogn)/// 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();priority_queue<int> pq;int i = 0, cur = startFuel, res = 0;while(cur < target){for(; i < n && cur >= stations[i][0]; i ++)pq.push(stations[i][1]);if(!pq.empty()) cur += pq.top(), pq.pop(), res ++;else break;}return cur >= target ? res : -1;}};int main() {int target1 = 1, startFuel1 = 1;vector<vector<int>> stations1;cout << Solution().minRefuelStops(target1, startFuel1, stations1) << endl;// 0int target2 = 100, startFuel2 = 1;vector<vector<int>> stations2 = {{10, 100}};cout << Solution().minRefuelStops(target2, startFuel2, stations2) << endl;// -1int target3 = 100, startFuel3 = 10;vector<vector<int>> stations3 = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};cout << Solution().minRefuelStops(target3, startFuel3, stations3) << endl;// 2int 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;// -1return 0;}