• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode871.最低加油次数|MinimumNumberofRefuelingStops

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10599859.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 = []
Output: 0
Explanation: We can reach the target without refueling.

Example 2:

Input: target = [[10,100]]
Output: -1
Explanation: We can't reach the target (or even the first gas station).

Example 3:

Input: target = [[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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift4.0从相册中获取图片和拍照发布时间:2022-07-13
下一篇:
[ios][swift]swift中声明空数组发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap