-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (29 loc) · 797 Bytes
/
Solution.cs
File metadata and controls
29 lines (29 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Solution
{
public int[] ShortestDistanceAfterQueries(int n, int[][] queries)
{
List<int>[] paths = new List<int>[n];
int[] dp = new int[n];
for (int i = 0; i < n - 1; ++i)
{
paths[i] = [i + 1];
dp[i] = i;
}
paths[n - 1] = [];
dp[n - 1] = n - 1;
int[] res = new int[queries.Length];
for (int i = 0; i < queries.Length; ++i)
{
paths[queries[i][0]].Add(queries[i][1]);
for (int j = queries[i][0]; j < n; ++j)
{
foreach (int next in paths[j])
{
dp[next] = Math.Min(dp[next], dp[j] + 1);
}
}
res[i] = dp[n - 1];
}
return res;
}
}