★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-gzrzvlsv-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A car travels from a starting position to a destination which is target
miles east of the starting position.git
Along the way, there are gas stations. Each station[i]
represents a gas station that is station[i][0]
miles east of the starting position, and has station[i][1]
liters of gas.github
The car starts with an infinite tank of gas, which initially has startFuel
liters of fuel in it. It uses 1 liter of gas per 1 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.app
What is the least number of refueling stops the car must make in order to reach its destination? If it cannot reach the destination, return -1
.ide
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. spa
Example 1:code
Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling.
Example 2:htm
Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station).
Example 3:blog
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.
Note:
1 <= target, startFuel, stations[i][1] <= 10^9
0 <= stations.length <= 500
0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target
英里处。
沿途有加油站,每一个 station[i]
表明一个加油站,它位于出发位置东面 station[i][0]
英里处,而且有 station[i][1]
升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel
升燃料。它每行驶 1 英里就会用掉 1 升汽油。
当汽车到达加油站时,它可能停下来加油,将全部汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?若是没法到达目的地,则返回 -1
。
注意:若是汽车到达加油站时剩余燃料为 0,它仍然能够在那里加油。若是汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。
示例 1:
输入:target = 1, startFuel = 1, stations = [] 输出:0 解释:咱们能够在不加油的状况下到达目的地。
示例 2:
输入:target = 100, startFuel = 1, stations = [[10,100]] 输出:-1 解释:咱们没法抵达目的地,甚至没法到达第一个加油站。
示例 3:
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] 输出:2 解释: 咱们出发时有 10 升燃料。 咱们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。 而后,咱们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料), 并将汽油从 10 升加到 50 升。而后咱们开车抵达目的地。 咱们沿途在1两个加油站停靠,因此返回 2 。
提示:
1 <= target, startFuel, stations[i][1] <= 10^9
0 <= stations.length <= 500
0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
1 class Solution { 2 func minRefuelStops(_ target: Int, _ startFuel: Int, _ stations: [[Int]]) -> Int { 3 if(startFuel >= target){ 4 return 0 5 } 6 var fuel = startFuel 7 var arriveStations:[Int] = [] 8 var stationNum = 0 9 var i = 0 10 while i < stations.count{ 11 if fuel >= stations[i][0]{ 12 if arriveStations.count > 0 { 13 for j in 0..<arriveStations.count{ 14 if stations[i][1] > arriveStations[j]{ 15 arriveStations.insert(stations[i][1], at: j) 16 break; 17 } 18 if j == arriveStations.count - 1 { 19 arriveStations.append(stations[i][1]) 20 } 21 } 22 }else{ 23 arriveStations.append(stations[i][1]) 24 } 25 i += 1 26 }else{ 27 if arriveStations.count > 0{ 28 fuel += arriveStations.first!; 29 arriveStations.removeFirst() 30 stationNum += 1; 31 if fuel >= target{ 32 return stationNum 33 } 34 }else{ 35 return -1 36 } 37 } 38 } 39 if fuel >= target{ 40 return stationNum 41 }else{ 42 while arriveStations.count > 0{ 43 fuel += arriveStations.first!; 44 arriveStations.removeFirst() 45 stationNum += 1; 46 if fuel >= target{ 47 return stationNum 48 } 49 } 50 } 51 return -1 52 } 53 }
1 class Solution { 2 func minRefuelStops(_ target: Int, _ startFuel: Int, _ stations: [[Int]]) -> Int { 3 let countS:Int = stations.count 4 var dp:[Int] = [Int](repeating:0,count:countS + 1) 5 dp[0] = startFuel 6 for i in 0..<countS 7 { 8 var t:Int = i 9 while(t >= 0 && dp[t] >= stations[i][0]) 10 { 11 dp[t + 1] = max(dp[t + 1], dp[t] + stations[i][1]) 12 t -= 1 13 } 14 } 15 for t in 0...countS 16 { 17 if dp[t] >= target 18 { 19 return t 20 } 21 } 22 return -1 23 } 24 }