leetcode - 3244. Shortest Distance After Road Addition Queries II

Description

You are given an integer n and a 2D integer array queries.

There are n cities numbered from 0 to n - 1. Initially, there is a unidirectional road from city i to city i + 1 for all 0 <= i < n - 1.

queriesi = ui, vi represents the addition of a new unidirectional road from city ui to city vi. After each query, you need to find the length of the shortest path from city 0 to city n - 1.

There are no two queries such that queriesi0 < queriesj0 < queriesi1 < queriesj1.

Return an array answer where for each i in the range 0, queries.length - 1, answeri is the length of the shortest path from city 0 to city n - 1 after processing the first i + 1 queries.

Example 1:

复制代码
Input: n = 5, queries = [[2,4],[0,2],[0,4]]

Output: [3,2,1]

Explanation:
复制代码
After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.
复制代码
After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.
复制代码
After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.

Example 2:

复制代码
Input: n = 4, queries = [[0,3],[0,2]]

Output: [1,1]

Explanation:
复制代码
After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.
复制代码
After the addition of the road from 0 to 2, the length of the shortest path remains 1.

Solution

Similar to 3243. Shortest Distance After Road Addition Queries I, but this time with more data and an additional rule: no overlapped queries.

So we have a tricky way to solve this, because we don't have overlapped queries, so we could just drop the nodes between each query. And the length of the graph would be our answer.

Here we use a hash map to denote the graph.

Time complexity: o ( n + q ) o(n+q) o(n+q)

Space complexity: o ( n ) o(n) o(n)

Code

python3 复制代码
class Solution:
    def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
        neighbors = {i: i + 1 for i in range(n - 1)}
        res = []
        for each_query in queries:
            start_city, end_city = each_query
            # if start_city is in the graph and the new query gives us a shorter way
            if start_city in neighbors and neighbors[start_city] < end_city:
                cur_city = neighbors[start_city]
                while cur_city < end_city:
                    cur_city = neighbors.pop(cur_city)
                neighbors[start_city] = end_city
            res.append(len(neighbors))
        return res
相关推荐
Benny_Tang11 分钟前
AT_arc180_d [ARC180D] Division into 3 题解
数据结构·c++·算法
殷色玫瑰25 分钟前
C++ STL:stack、queue、priority_queue 与容器适配器详解
java·开发语言·数据结构·c++·算法·visualstudio
Evand J1 小时前
【电机滤波例程1】PMSM永磁同步电机转速与位置估计,MATLAB程序:标准无迹卡尔曼滤波、模型说明与仿真验证
算法·matlab·电机·ekf·负载·卡尔曼滤波
我命由我123451 小时前
Photoshop - Photoshop 快速选择和更改部分图像内容
学习·职场和发展·求职招聘·职场发展·学习方法·photoshop·ps
我命由我123451 小时前
Photoshop - 了解 Photoshop
学习·职场和发展·产品运营·求职招聘·职场发展·学习方法·photoshop
打码的老程是远篁1 小时前
手把手教你玩转大模型——3. Transformer 的位置编码——模型怎么知道谁在前,谁在后?
人工智能·算法
嘎嘎风1 小时前
MiniMind 学习笔记之 06 一张图、一百行:MiniMind 的骨架
算法·源码
用户7341681035481 小时前
只盯被动响应做状态监测?论“控制信号”在变工况故障诊断中的重要作用
算法
Ivanqhz1 小时前
MQA、GQA、稀疏/滑动窗口注意力及混合范式
人工智能·算法·机器学习
罗湖老棍子2 小时前
荒岛野人(信息学奥赛一本通- P1637)(洛谷-P2421)
算法·数论·枚举·裴蜀定理·扩展欧几里得·扩展中国剩余定理