LeetCode 题解工作台
前往目标的最小代价
给你一个数组 start ,其中 start = [startX, startY] 表示你的初始位置位于二维空间上的 (startX, startY) 。另给你一个数组 target ,其中 target = [targetX, targetY] 表示你的目标位置 (targetX, targetY…
4
题型
5
代码语言
3
相关题
当前训练重点
中等 · 堆
答案摘要
我们可以发现,对于访问到的每个坐标点 $(x, y)$,假设从起点到 $(x, y)$ 的最小代价为 。如果选择直接移动到 $(targetX, targetY)$,那么总代价就是 $d + |x - targetX| + |y - targetY|$。如果选择经过某条特殊路径 $(x_1, y_1) \rightarrow (x_2, y_2)$,那么我们需要可以花费 $|x - x_1| + …
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 堆 题型思路
题目描述
给你一个数组 start ,其中 start = [startX, startY] 表示你的初始位置位于二维空间上的 (startX, startY) 。另给你一个数组 target ,其中 target = [targetX, targetY] 表示你的目标位置 (targetX, targetY) 。
从位置 (x1, y1) 到空间中任一其他位置 (x2, y2) 的 代价 是 |x2 - x1| + |y2 - y1| 。
给你一个二维数组 specialRoads ,表示空间中存在的一些 特殊路径。其中 specialRoads[i] = [x1i, y1i, x2i, y2i, costi] 表示第 i 条特殊路径可以从 (x1i, y1i) 到 (x2i, y2i) ,但成本等于 costi 。你可以使用每条特殊路径任意次数。
返回从 (startX, startY) 到 (targetX, targetY) 所需的 最小 代价。
示例 1:
输入:start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
输出:5
解释:
- (1,1) 到 (1,2) 花费为 |1 - 1| + |2 - 1| = 1。
- (1,2) 到 (3,3)。使用
specialRoads[0]花费为 2。 - (3,3) 到 (3,4) 花费为 |3 - 3| + |4 - 3| = 1。
- (3,4) 到 (4,5)。使用
specialRoads[1]花费为 1。
所以总花费是 1 + 2 + 1 + 1 = 5。
示例 2:
输入:start = [3,2], target = [5,7], specialRoads = [[5,7,3,2,1],[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
输出:7
解释:
不使用任何特殊路径,直接从开始到结束位置是最优的,花费为 |5 - 3| + |7 - 2| = 7。
注意 specialRoads[0] 直接从 (5,7) 到 (3,2)。
示例 3:
输入:start = [1,1], target = [10,4], specialRoads = [[4,2,1,1,3],[1,2,7,4,4],[10,3,6,1,2],[6,1,1,2,3]]
输出:8
解释:
- (1,1) 到 (1,2) 花费为 |1 - 1| + |2 - 1| = 1。
- (1,2) 到 (7,4)。使用
specialRoads[1]花费为 4。 - (7,4) 到 (10,4) 花费为 |10 - 7| + |4 - 4| = 3。
提示:
start.length == target.length == 21 <= startX <= targetX <= 1051 <= startY <= targetY <= 1051 <= specialRoads.length <= 200specialRoads[i].length == 5startX <= x1i, x2i <= targetXstartY <= y1i, y2i <= targetY1 <= costi <= 105
解题思路
方法一:Dijkstra
我们可以发现,对于访问到的每个坐标点 ,假设从起点到 的最小代价为 。如果选择直接移动到 ,那么总代价就是 。如果选择经过某条特殊路径 ,那么我们需要可以花费 的代价,从 移动到 。
因此,我们可以使用 Dijkstra 算法求出从起点到所有点的最小代价,然后从中选择最小的那个。
我们定义一个优先队列 ,队列中的每一个元素是一个三元组 ,表示从起点到 的最小代价为 。初始时,我们将 加入队列中。
在每一步中,我们取出队首元素 ,此时我们可以更新答案,即 。然后我们枚举所有的特殊路径 ,将 加入队列中。
最后当队列为空时,我们就可以得到答案。
时间复杂度 ,空间复杂度 。其中 是特殊路径的数量。
class Solution:
def minimumCost(
self, start: List[int], target: List[int], specialRoads: List[List[int]]
) -> int:
def dist(x1: int, y1: int, x2: int, y2: int) -> int:
return abs(x1 - x2) + abs(y1 - y2)
q = [(0, start[0], start[1])]
vis = set()
ans = inf
while q:
d, x, y = heappop(q)
if (x, y) in vis:
continue
vis.add((x, y))
ans = min(ans, d + dist(x, y, *target))
for x1, y1, x2, y2, cost in specialRoads:
heappush(q, (d + dist(x, y, x1, y1) + cost, x2, y2))
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Evaluating how the candidate handles pathfinding in a graph with variable edge costs.
- question_mark
How the candidate optimizes the graph search based on the start and target points.
- question_mark
Whether the candidate can apply Dijkstra's algorithm correctly with the given constraints.
常见陷阱
外企场景- error
Not reducing the problem space by only considering key points (start, target, special road endpoints).
- error
Ignoring the optimization of pathfinding using a priority queue, leading to inefficient solutions.
- error
Failing to handle edge cases such as when no special roads are useful or when a direct path is optimal.
进阶变体
外企场景- arrow_right_alt
Consider adding more special roads with varied costs to increase the complexity of the problem.
- arrow_right_alt
Modify the problem to allow for multiple target positions and find the shortest path to any of them.
- arrow_right_alt
Allow bidirectional special roads, which would require additional logic to handle the reverse direction.