[LeetCode] 871. Minimum Number of Refueling Stops
作者:互联网
A car travels from a starting position to a destination which is target
miles east of the starting position.
There are gas stations along the way. The gas stations are represented as an array stations
where stations[i] = [positioni, fueli]
indicates that the ith
gas station is positioni
miles east of the starting position and has fueli
liters of gas.
The car starts with an infinite tank of gas, which initially has startFuel
liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1
.
Note that if the car reaches a gas station with 0
fuel left, the car can still refuel there. If the car reaches the destination with 0
fuel left, it is still considered to have arrived.
Example 1:
Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling.
Example 2:
Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can not reach the target (or even the first gas station).
Example 3:
Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.
Constraints:
1 <= target, startFuel <= 109
0 <= stations.length <= 500
0 <= positioni <= positioni+1 < target
1 <= fueli < 109
最低加油次数。
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。
沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。
注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-number-of-refueling-stops
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意告诉我们有若干个加油站,给定了每个加油站的位置和油量,同时我们有一个起始油量,看看我们最少能加油多少次且到达目的地。这道题有两种思路,一种是贪心,一种是动态规划。
贪心的做法是我们用一个最大堆记录路上的所有加油站的油量,最大的油量在堆顶。我们再用一个变量 maxDistance 记录汽车能到达的最远距离,如果汽车到不了下一个加油站,就说明我们必须要加油了。此时我们加一个最大的油量(贪心),对加油次数++,看是否能到下一个加油站。如果加一次不够,我们就再加,直到能到达下一个加油站。但是如果在行进过程中最大堆直接就空了,就说明我们无法到达目的地,就返回 -1。
时间O(nlogn)
空间O(n)
Java实现
1 class Solution { 2 public int minRefuelStops(int target, int startFuel, int[][] stations) { 3 // corner case 4 if (startFuel >= target) { 5 return 0; 6 } 7 8 // normal case 9 PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a); 10 int maxDistance = startFuel; 11 int stops = 0; 12 int i = 0; 13 while (maxDistance < target) { 14 while (i < stations.length && stations[i][0] <= maxDistance) { 15 queue.offer(stations[i][1]); 16 i++; 17 } 18 if (queue.isEmpty()) { 19 return -1; 20 } 21 maxDistance += queue.poll(); 22 stops++; 23 } 24 return stops; 25 } 26 }
动态规划的做法也需要掌握。这里我们需要一个二维数组记录DP的中间结果,dp 的定义是到达第 i 个加油站,加了 j 次油能跑的最远距离。
初始化的值是对所有的 dp[i][0] = startFuel,意思是如果我们过程中一直不加油,能到的最远距离就只是一开始的油量 startFuel。
对于从 index == 1 开始之后所有的加油站,我们都有两种情况,加油或者不加油。
对于当前加油站 i,如果我们能到达这里且在这里不加油,能跑的最远距离 = dp[i - 1][j],意思是如果在当前加油站不加油,最远距离 = 在上一个加油站计算过的能跑的最远距离。
对于当前加油站 i,如果我们能到达这里且在这里加油,能跑的最远距离 = dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + curGas),意思是如果在当前加油站加油,最远距离 = 在在上一个加油站计算过的能跑的最远距离 + 在当前加油站加过油的距离。
计算完所有的 dp 值之后,我们遍历一下 dp 数组的最后一行 dp[len][i],从小到大扫描,看最少需要几次加油就能到达这里。
时间O(nlogn)
空间O(n)
Java实现
1 class Solution { 2 public int minRefuelStops(int target, int startFuel, int[][] stations) { 3 // corner case 4 if (startFuel >= target) { 5 return 0; 6 } 7 8 // normal case 9 int n = stations.length; 10 // dp[i][j]表示经过i站加油j次能够到达的最远距离 11 int[][] dp = new int[n + 1][n + 1]; 12 // 无论在哪一个加油站,不加油,就只能走startFuel的距离 13 for (int i = 0; i < n; i++) { 14 dp[i][0] = startFuel; 15 } 16 17 for (int i = 1; i <= n; i++) { 18 int curPosition = stations[i - 1][0]; 19 int curGas = stations[i - 1][1]; 20 for (int j = 1; j <= i; j++) { 21 // 本站不加油 22 if (dp[i - 1][j] >= curPosition) { 23 dp[i][j] = dp[i - 1][j]; 24 } 25 // 本站加油 26 if (dp[i - 1][j - 1] >= curPosition) { 27 dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + curGas); 28 } 29 } 30 } 31 32 for (int stops = 0; stops <= n; stops++) { 33 if (dp[n][stops] >= target) { 34 return stops; 35 } 36 } 37 return -1; 38 } 39 }
标签:target,int,Number,Stops,871,加油站,startFuel,加油,dp 来源: https://www.cnblogs.com/cnoodle/p/16436379.html