• 【leetcode】871. Minimum Number of Refueling Stops


    题目如下:

    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

    解题思路:记dp[i][j]为车辆在第i个加油站做第j次停靠,并且加完了第i个加油站的油后可以获得的最大的油的数量。那么可以和第j-1次停靠建立状态转移方程,有dp[i][j] = max(stations[i][1] + dp[k][j-1] - (stations[i][0] - stations[k][0])) ,其中k表示第j-1次停靠的站点,有k < i。这里还需要满足一个条件,就是  dp[k][j-1]  >=  stations[i][0] - stations[k][0],表示有足够的油可以使得汽车从第k个加油站行驶到第i个加油站。但是这样存在i,j,k三个变量,时间复杂度是O(n^3)。需要继续优化,观察dp[i][j] = max(stations[i][1] + dp[k][j-1] - (stations[i][0] - stations[k][0])),发现stations[i][0] 和stations[i][1]都是固定的,变化的只有 dp[k][j-1] +  stations[k][0] ,所以只需要用max_dp[i][j]记录从0~i,0~j区间内 dp[i][j] +  stations[i][0] 的最大值,即可将时间复杂度降为O(n^2)。最后遍历 dp,找出最小的j使得dp[i][j] >= (target - stations[i][0]) 即可。

    代码如下:

    class Solution(object):
        def minRefuelStops(self, target, startFuel, stations):
            """
            :type target: int
            :type startFuel: int
            :type stations: List[List[int]]
            :rtype: int
            """
            if startFuel >= target:
                return 0
            elif len(stations) == 0 and target > startFuel:
                return -1
            dp = [[-float('inf')] * len(stations) for _ in stations]
            v = startFuel - stations[0][0]
            if v < 0:return -1
            dp[0][0] = v + stations[0][1]
    
            max_dp = [[-float('inf')] * len(stations) for _ in stations]  # max sum of dp[k][j-1] + stations[k][0]
    
            max_dp[0][0] = dp[0][0] + stations[0][0]
    
            # dp[i][j]:the i station, the j times stop
    
            res = float('inf')
            if dp[0][0] >= (target - stations[0][0]):res = 0
    
            for i in range(1,len(stations)):
                if target < stations[i][1]:
                    break
                for j in range(min(res,i+1)):
                    max_dp[i][j] = max_dp[i-1][j]
                    if j == 0:
                        v = startFuel - stations[i][0]
                        if v >= 0:
                            dp[i][j] = max(dp[i][j], stations[i][1] + startFuel - stations[i][0])
                    else:
                        # dp[i][j] = max(stations[i][1] + dp[k][j-1] - (stations[i][0] - stations[k][0]))   (k<i)
                        # --->
                        # dp[i][j] = stations[i][1] - stations[i][0] + max(dp[k][j-1] +  stations[k][0])  (k <i)
                        # ---> let max_dp[i][j-1] = max(dp[k][j-1] +  stations[k][0]) (k<=i)
    
                        #
                        if max_dp[i-1][j-1]  - stations[i][0] >= 0:
                            dp[i][j] = max(dp[i][j], max_dp[i-1][j-1] + stations[i][1] - stations[i][0])
                    if dp[i][j] >= 0:
                        max_dp[i][j] = max(max_dp[i-1][j], dp[i][j] + stations[i][0] )
                    if dp[i][j] >= (target - stations[i][0]):
                        res = min(res,j)
    
            return res + 1 if res != float('inf') else -1
  • 相关阅读:
    教师节快乐!
    来自学长同学分享的学习方法
    刘奕佳: 我的职校新生活 | 班级日常分享
    19级:班级日常分享 | 一天一瞬间
    珍惜、珍爱!
    19级:班级日常分享 | 一天一瞬间
    204787 ,194787 |0001 1131 0001 4226 7035 ![2480 ]
    1-N的自然数中,少了一个,找出这个数 小强斋
    获取某一天之前或者之后多少天的日期 小强斋
    获取某一天之前或者之后多少天的日期 小强斋
  • 原文地址:https://www.cnblogs.com/seyjs/p/12170976.html
Copyright © 2020-2023  润新知