码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

时间:2019-07-15 01:31:09      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:HERE   blog   imu   eve   input   present   ring   class   cannot   



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:

  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



Github 同步地址:

https://github.com/grandyang/leetcode/issues/871



参考资料:

https://leetcode.com/problems/minimum-number-of-refueling-stops/



LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

标签:HERE   blog   imu   eve   input   present   ring   class   cannot   

原文地址:https://www.cnblogs.com/grandyang/p/11186533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!