LeetCode 题解工作台

最低加油次数

汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。 沿途有加油站,用数组 stations 表示。其中 stations[i] = [position i , fuel i ] 表示第 i 个加油站位于出发位置东面 position i 英里处,并且有 fuel i 升汽油。…

category

4

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们可以利用优先队列(大根堆) 记录所有已经到达过的加油站的加油量,每次当油量不足时,贪心地取出最大加油量,即 的堆顶元素,并累计加油次数 。如果 为空并且当前油量仍然不足,说明无法到达目的地,返回 。 时间复杂度 $O(n \times \log n)$,空间复杂度 。其中 表示加油站的数量。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。

沿途有加油站,用数组 stations 表示。其中 stations[i] = [positioni, fueli] 表示第 i 个加油站位于出发位置东面 positioni 英里处,并且有 fueli 升汽油。

假设汽车油箱的容量是无限的,其中最初有 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 升。然后开车抵达目的地。
沿途在两个加油站停靠,所以返回 2 。

 

提示:

  • 1 <= target, startFuel <= 109
  • 0 <= stations.length <= 500
  • 1 <= positioni < positioni+1 < target
  • 1 <= fueli < 109
lightbulb

解题思路

方法一:贪心 + 优先队列(大根堆)

我们可以利用优先队列(大根堆) pq\textit{pq} 记录所有已经到达过的加油站的加油量,每次当油量不足时,贪心地取出最大加油量,即 pq\textit{pq} 的堆顶元素,并累计加油次数 ans\textit{ans}。如果 pq\textit{pq} 为空并且当前油量仍然不足,说明无法到达目的地,返回 1-1

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 表示加油站的数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def minRefuelStops(
        self, target: int, startFuel: int, stations: List[List[int]]
    ) -> int:
        pq = []
        ans = pre = 0
        stations.append([target, 0])
        for pos, fuel in stations:
            dist = pos - pre
            startFuel -= dist
            while startFuel < 0 and pq:
                startFuel -= heappop(pq)
                ans += 1
            if startFuel < 0:
                return -1
            heappush(pq, -fuel)
            pre = pos
        return ans
speed

复杂度分析

指标
时间complexity is O(N \log N) due to heap operations or DP updates for N stations. Space complexity is O(N) to store DP array or heap elements, reflecting fuel choices at each station.
空间O(N)
psychology

面试官常问的追问

外企场景
  • question_mark

    Asks about edge cases with no stations and minimal start fuel.

  • question_mark

    Explores the trade-off between DP updates versus greedy max-heap optimization.

  • question_mark

    Wants explanation on why state transition DP captures minimal refueling stops.

warning

常见陷阱

外企场景
  • error

    Failing to handle cases where startFuel is enough to reach target without refueling.

  • error

    Not iterating DP array backwards leading to incorrect state updates.

  • error

    Using greedy selection without a heap can underestimate required refueling stops.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Compute maximum distance reachable with at most K refueling stops.

  • arrow_right_alt

    Determine minimum stops if fuel consumption varies per mile.

  • arrow_right_alt

    Optimize for fuel cost differences at each station instead of quantity.

help

常见问题

外企场景

最低加油次数题解:状态·转移·动态规划 | LeetCode #871 困难