|
| 1 | +package dynamic_programming; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 24/07/2018. |
| 5 | + * A car travels from a starting position to a destination which is target miles east of the starting position. |
| 6 | +
|
| 7 | + Along the way, there are gas stations. Each station[i] represents a gas station that is station[i][0] miles east |
| 8 | + of the starting position, and has station[i][1] liters of gas. |
| 9 | +
|
| 10 | + The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses 1 liter of |
| 11 | + gas per 1 mile that it drives. |
| 12 | +
|
| 13 | + When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. |
| 14 | +
|
| 15 | + What is the least number of refueling stops the car must make in order to reach its destination? If it cannot reach |
| 16 | + the destination, return -1. |
| 17 | +
|
| 18 | + Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the |
| 19 | + destination with 0 fuel left, it is still considered to have arrived. |
| 20 | +
|
| 21 | +
|
| 22 | +
|
| 23 | + Example 1: |
| 24 | +
|
| 25 | + Input: target = 1, startFuel = 1, stations = [] |
| 26 | + Output: 0 |
| 27 | + Explanation: We can reach the target without refueling. |
| 28 | + Example 2: |
| 29 | +
|
| 30 | + Input: target = 100, startFuel = 1, stations = [[10,100]] |
| 31 | + Output: -1 |
| 32 | + Explanation: We can't reach the target (or even the first gas station). |
| 33 | + Example 3: |
| 34 | +
|
| 35 | + Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] |
| 36 | + Output: 2 |
| 37 | + Explanation: |
| 38 | + We start with 10 liters of fuel. |
| 39 | + We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. |
| 40 | + Then, we drive from position 10 to position 60 (expending 50 liters of fuel), |
| 41 | + and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. |
| 42 | + We made 2 refueling stops along the way, so we return 2. |
| 43 | +
|
| 44 | +
|
| 45 | + Note: |
| 46 | +
|
| 47 | + 1 <= target, startFuel, stations[i][1] <= 10^9 |
| 48 | + 0 <= stations.length <= 500 |
| 49 | + 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target |
| 50 | +
|
| 51 | + Solution O(N ^ 2): Maintain a DP array with maximum distance that can be travelled with i stops. |
| 52 | + DP[i] is the max distance that can be travelled with exactly i stops. |
| 53 | + The minimum i where the target can be achieved (dp[i] >= target) will be the answer. |
| 54 | + */ |
| 55 | +public class MinimumNumberOfRefuelingStops { |
| 56 | + |
| 57 | + /** |
| 58 | + * Main method |
| 59 | + * @param args |
| 60 | + */ |
| 61 | + public static void main(String[] args) { |
| 62 | + int target = 100, startFuel = 10; |
| 63 | + int[][] stations = {{10,60},{20,30},{30,30},{60,40}}; |
| 64 | + System.out.println(new MinimumNumberOfRefuelingStops().minRefuelStops(target, startFuel, stations)); |
| 65 | + } |
| 66 | + |
| 67 | + public int minRefuelStops(int target, int startFuel, int[][] stations) { |
| 68 | + long[] dp = new long[stations.length + 1]; |
| 69 | + dp[0] = startFuel; |
| 70 | + for(int i = 0; i < stations.length; i ++){ |
| 71 | + int d = stations[i][0]; |
| 72 | + int f = stations[i][1]; |
| 73 | + for(int j = i; j >= 0; j --){ |
| 74 | + if(dp[j] >= d){ |
| 75 | + dp[j + 1] = Math.max(dp[j + 1], dp[j] + f); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + for(int i = 0; i < dp.length; i ++){ |
| 80 | + if(dp[i] >= target){ |
| 81 | + return i; |
| 82 | + } |
| 83 | + } |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments