|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +You are given an integer n and a 2D integer array queries. |
| 4 | +
|
| 5 | +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. |
| 6 | +
|
| 7 | +queries[i] = [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. |
| 8 | +
|
| 9 | +Return an array answer where for each i in the range [0, queries.length - 1], answer[i] is the length of the shortest path from city 0 to city n - 1 after processing the first i + 1 queries. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: n = 5, queries = [[2,4],[0,2],[0,4]] |
| 16 | +
|
| 17 | +Output: [3,2,1] |
| 18 | +
|
| 19 | +Explanation: |
| 20 | +
|
| 21 | +
|
| 22 | +
|
| 23 | +After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3. |
| 24 | +
|
| 25 | +
|
| 26 | +
|
| 27 | +After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2. |
| 28 | +
|
| 29 | +
|
| 30 | +
|
| 31 | +After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1. |
| 32 | +
|
| 33 | +Example 2: |
| 34 | +
|
| 35 | +Input: n = 4, queries = [[0,3],[0,2]] |
| 36 | +
|
| 37 | +Output: [1,1] |
| 38 | +
|
| 39 | +Explanation: |
| 40 | +
|
| 41 | +
|
| 42 | +
|
| 43 | +After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1. |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +After the addition of the road from 0 to 2, the length of the shortest path remains 1. |
| 48 | +
|
| 49 | + |
| 50 | +
|
| 51 | +Constraints: |
| 52 | +
|
| 53 | +3 <= n <= 500 |
| 54 | +1 <= queries.length <= 500 |
| 55 | +queries[i].length == 2 |
| 56 | +0 <= queries[i][0] < queries[i][1] < n |
| 57 | +1 < queries[i][1] - queries[i][0] |
| 58 | +There are no repeated roads among the queries. |
| 59 | +""" |
| 60 | +from collections import defaultdict, deque |
| 61 | + |
| 62 | + |
| 63 | +class Solution: |
| 64 | + def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]: |
| 65 | + G = defaultdict(list) |
| 66 | + dist = [] |
| 67 | + for i in range(n-1): |
| 68 | + G[i].append(i+1) |
| 69 | + |
| 70 | + for i in range(n): |
| 71 | + dist.append(i) |
| 72 | + |
| 73 | + ret = [] |
| 74 | + for u, v in queries: |
| 75 | + G[u].append(v) |
| 76 | + self.bfs(G, dist, u) |
| 77 | + ret.append(dist[~0]) |
| 78 | + |
| 79 | + return ret |
| 80 | + |
| 81 | + def bfs(self, G, dist, s): |
| 82 | + """ |
| 83 | + * Known origin and end destination |
| 84 | + * dist is the distance from origin, not to destination |
| 85 | + * BFS update the distance from the source, where the source is the |
| 86 | + start of the new edge, not the origin of the graph |
| 87 | + """ |
| 88 | + q = deque() |
| 89 | + q.append(s) |
| 90 | + while q: |
| 91 | + v = q.popleft() |
| 92 | + for nbr in G[v]: |
| 93 | + if dist[nbr] > dist[v] + 1: |
| 94 | + # It and its descendants require distance update |
| 95 | + dist[nbr] = dist[v] + 1 |
| 96 | + q.append(nbr) |
| 97 | + |
| 98 | + |
| 99 | +class SolutionTLE: |
| 100 | + def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]: |
| 101 | + """ |
| 102 | + Naive solution: |
| 103 | + 1. maintain a graph |
| 104 | + 2. BFS |
| 105 | + """ |
| 106 | + G = defaultdict(list) |
| 107 | + for i in range(n-1): |
| 108 | + G[i].append(i+1) |
| 109 | + |
| 110 | + ret = [] |
| 111 | + for q in queries: |
| 112 | + s, t = q |
| 113 | + G[s].append(t) |
| 114 | + ret.append(self.bfs(G, 0, n - 1)) |
| 115 | + |
| 116 | + return ret |
| 117 | + |
| 118 | + def bfs(self, G, s, t): |
| 119 | + q = [s] |
| 120 | + ret = 0 |
| 121 | + while q: |
| 122 | + nxt_q = [] |
| 123 | + ret += 1 |
| 124 | + for v in q: |
| 125 | + for nbr in G[v]: |
| 126 | + if nbr == t: |
| 127 | + return ret |
| 128 | + nxt_q.append(nbr) |
| 129 | + |
| 130 | + q = nxt_q |
0 commit comments