LeetCode 题解工作台

新增道路查询后的最短距离 II

给你一个整数 n 和一个二维整数数组 queries 。 有 n 个城市,编号从 0 到 n - 1 。初始时,每个城市 i 都有一条 单向 道路通往城市 i + 1 ( 0 )。 queries[i] = [u i , v i ] 表示新建一条从城市 u i 到城市 v i 的 单向 道路。每次查…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 贪心·invariant

bolt

答案摘要

我们定义一个长度为 $n - 1$ 的数组 ,其中 表示从城市 可以到达的下一个城市的编号。初始时 $\textit{nxt}[i] = i + 1$。 对于每次查询 $[u, v]$,如果此前已经连通了 和 ,且 $u' <= u < v <= v'$,那么我们可以跳过这次查询。否则,我们需要将 到 $nxt[v - 1]$ 这些城市的下一个城市编号设置为 ,并将 设置为 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数 n 和一个二维整数数组 queries

n 个城市,编号从 0n - 1。初始时,每个城市 i 都有一条单向道路通往城市 i + 10 <= i < n - 1)。

queries[i] = [ui, vi] 表示新建一条从城市 ui 到城市 vi单向道路。每次查询后,你需要找到从城市 0 到城市 n - 1最短路径长度

所有查询中不会存在两个查询都满足 queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]

返回一个数组 answer,对于范围 [0, queries.length - 1] 中的每个 ianswer[i] 是处理完 i + 1 个查询后,从城市 0 到城市 n - 1 的最短路径的长度

 

示例 1:

输入: n = 5, queries = [[2, 4], [0, 2], [0, 4]]

输出: [3, 2, 1]

解释:

新增一条从 2 到 4 的道路后,从 0 到 4 的最短路径长度为 3。

新增一条从 0 到 2 的道路后,从 0 到 4 的最短路径长度为 2。

新增一条从 0 到 4 的道路后,从 0 到 4 的最短路径长度为 1。

示例 2:

输入: n = 4, queries = [[0, 3], [0, 2]]

输出: [1, 1]

解释:

新增一条从 0 到 3 的道路后,从 0 到 3 的最短路径长度为 1。

新增一条从 0 到 2 的道路后,从 0 到 3 的最短路径长度仍为 1。

 

提示:

  • 3 <= n <= 105
  • 1 <= queries.length <= 105
  • queries[i].length == 2
  • 0 <= queries[i][0] < queries[i][1] < n
  • 1 < queries[i][1] - queries[i][0]
  • 查询中不存在重复的道路。
  • 不存在两个查询都满足 i != jqueries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]
lightbulb

解题思路

方法一:贪心 + 记录跳转位置

我们定义一个长度为 n1n - 1 的数组 nxt\textit{nxt},其中 nxt[i]\textit{nxt}[i] 表示从城市 ii 可以到达的下一个城市的编号。初始时 nxt[i]=i+1\textit{nxt}[i] = i + 1

对于每次查询 [u,v][u, v],如果此前已经连通了 uu'vv',且 u<=u<v<=vu' <= u < v <= v',那么我们可以跳过这次查询。否则,我们需要将 nxt[u]nxt[u]nxt[v1]nxt[v - 1] 这些城市的下一个城市编号设置为 00,并将 nxt[u]nxt[u] 设置为 vv

在这个过程中,我们维护一个变量 cnt\textit{cnt},表示从城市 00 到城市 n1n - 1 的最短路径的长度。初始时 cnt=n1\textit{cnt} = n - 1。每一次,如果我们将 [nxt[u],v)[\textit{nxt}[u], \textit{v}) 这些城市的下一个城市编号设置为 00,那么 cnt\textit{cnt} 就会减少 11

时间复杂度 O(n+q)O(n + q),空间复杂度 O(n)O(n)。其中 nnqq 分别是城市数量和查询数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
    def shortestDistanceAfterQueries(
        self, n: int, queries: List[List[int]]
    ) -> List[int]:
        nxt = list(range(1, n))
        ans = []
        cnt = n - 1
        for u, v in queries:
            if 0 < nxt[u] < v:
                i = nxt[u]
                while i < v:
                    cnt -= 1
                    nxt[i], i = 0, nxt[i]
                nxt[u] = v
            ans.append(cnt)
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate can handle graph traversal problems with dynamic updates.

  • question_mark

    Look for understanding of greedy algorithms and how they can be applied to graph-related problems.

  • question_mark

    Assess how efficiently the candidate manages graph updates to avoid excessive recomputation.

warning

常见陷阱

外企场景
  • error

    Not updating the graph efficiently, leading to redundant recalculations of the shortest path.

  • error

    Forgetting to optimize the process for larger inputs, leading to timeouts.

  • error

    Misunderstanding the greedy choice strategy, which may result in suboptimal path calculations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Add more roads at once rather than one at a time, requiring batch processing.

  • arrow_right_alt

    Allow roads to be bi-directional, adding complexity to the shortest path calculations.

  • arrow_right_alt

    Introduce more constraints such as varying road lengths, instead of assuming unit length roads.

help

常见问题

外企场景

新增道路查询后的最短距离 II题解:贪心·invariant | LeetCode #3244 困难