[Swift]LeetCode871. 最低加油次數 | Minimum Number of Refueling Stops

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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. 1 <= target, startFuel, stations[i][1] <= 10^9
  2. 0 <= stations.length <= 500
  3. 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. 1 <= target, startFuel, stations[i][1] <= 10^9
  2. 0 <= stations.length <= 500
  3. 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target

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 }
相關文章
相關標籤/搜索