在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A car travels from a starting position to a destination which is Along the way, there are gas stations. Each The car starts with an infinite tank of gas, which initially has 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 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:
汽车从起点出发驶向目的地,该目的地位于出发位置东面 沿途有加油站,每个 假设汽车油箱的容量是无限的,其中最初有 当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。 为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 注意:如果汽车到达加油站时剩余燃料为 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 }
|
请发表评论