[Swift]LeetCode871. 最低加油次数 | Minimum Number of Refueling Stops
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10599859.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
A car travels from a starting position to a destination which is?target?miles east of the starting position.
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.
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.
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.
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't 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.?Note:
汽車從起點出發駛向目的地,該目的地位于出發位置東面?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 。?提示:
Runtime:?216 ms Memory Usage:?19.2 MB 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 }
Runtime:?496 ms Memory Usage:?18.8 MB 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 }
?
轉載于:https://www.cnblogs.com/strengthen/p/10599859.html
總結
以上是生活随笔為你收集整理的[Swift]LeetCode871. 最低加油次数 | Minimum Number of Refueling Stops的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS中利用BootStrap框架引入g
- 下一篇: unity3d meshBaker 基本