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.
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.
There are no two queries such that queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1].
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
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 4is3.
After the addition of the road from 0 to 2, the length of the shortest path
from 0 to 4is2.
After the addition of the road from 0 to 4, the length of the shortest path
from 0 to 4is1.
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 3is1.
After the addition of the road from 0 to 2, the length of the shortest path
remains 1.
The shortest path from city 0 to city n-1 is initially n-1 (the direct chain). Each query adds a shortcut (ui, vi), which can only reduce the path if it creates a shorter jump. Since queries never overlap in a nested way, we can greedily keep track of the shortest jump to n-1.
classSolution {
public: vector<int> shortestPath(int n, vector<vector<int>>& queries) {
int ans = n -1;
vector<int> res;
for (auto& q : queries) {
int u = q[0], v = q[1];
ans = min(ans, u +1+ (n -1- v));
res.push_back(ans);
}
return res;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funcshortestPath(nint, queries [][]int) []int {
ans:=n-1res:= make([]int, 0, len(queries))
for_, q:=rangequeries {
u, v:=q[0], q[1]
ifu+1+(n-1-v) < ans {
ans = u+1+(n-1-v)
}
res = append(res, ans)
}
returnres}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
public List<Integer>shortestPath(int n, List<List<Integer>> queries) {
int ans = n - 1;
List<Integer> res =new ArrayList<>();
for (List<Integer> q : queries) {
int u = q.get(0), v = q.get(1);
ans = Math.min(ans, u + 1 + (n - 1 - v));
res.add(ans);
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funshortestPath(n: Int, queries: List<List<Int>>): List<Int> {
var ans = n - 1val res = mutableListOf<Int>()
for (q in queries) {
val u = q[0]
val v = q[1]
ans = minOf(ans, u + 1 + (n - 1 - v))
res.add(ans)
}
return res
}
}
1
2
3
4
5
6
7
8
classSolution:
defshortestPath(self, n: int, queries: list[list[int]]) -> list[int]:
ans = n -1 res: list[int] = []
for u, v in queries:
ans = min(ans, u +1+ (n -1- v))
res.append(ans)
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnshortest_path(n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
letmut ans = n -1;
letmut res = Vec::with_capacity(queries.len());
for q in queries {
let u = q[0];
let v = q[1];
ans = ans.min(u +1+ (n -1- v));
res.push(ans);
}
res
}
}