Shortest Distance After Road Addition Queries II - Problem

Imagine you're a city planner managing a transportation network! You have n cities numbered from 0 to n-1, initially connected by a simple highway system where each city i has a direct road to city i+1.

Now comes the exciting part: you'll receive a series of road construction queries, where each query [u, v] represents building a new express highway from city u directly to city v. These new roads can create shortcuts that dramatically reduce travel time!

Your mission is to track how these new roads affect the shortest path from city 0 to city n-1 after each construction project. The key constraint is that the queries follow a specific pattern: no two queries will create overlapping or intersecting shortcuts.

Goal: Return an array where each element represents the shortest distance from city 0 to city n-1 after processing each query in sequence.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 5, queries = [[2,4],[0,2],[0,4]]
โ€บ Output: [3,2,1]
๐Ÿ’ก Note: Initially: 0โ†’1โ†’2โ†’3โ†’4 (distance 4). After [2,4]: 0โ†’1โ†’2โ†’4 (distance 3). After [0,2]: 0โ†’2โ†’4 (distance 2). After [0,4]: 0โ†’4 (distance 1).
example_2.py โ€” Single Query
$ Input: n = 4, queries = [[0,3]]
โ€บ Output: [1]
๐Ÿ’ก Note: Initially: 0โ†’1โ†’2โ†’3 (distance 3). After [0,3]: 0โ†’3 (distance 1). The direct road creates a shortcut.
example_3.py โ€” No Improvement
$ Input: n = 4, queries = [[0,1],[1,2]]
โ€บ Output: [3,3]
๐Ÿ’ก Note: Both queries add roads that already exist in the optimal path, so the distance remains 3.

Constraints

  • 3 โ‰ค n โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 105
  • queries[i].length == 2
  • 0 โ‰ค queries[i][0] < queries[i][1] < n
  • No two queries create intersecting shortcuts
  • queries[i][0] โ‰  0 or queries[i][1] โ‰  n - 1 for more than one query

Visualization

Tap to expand
Highway System OptimizationInitialQuery 1Query 2OptimizedDistance: 401234Distance: 201234Express 0โ†’3Union-Find MagicUnion-Find Tracks:โ€ข Bypassed citiesโ€ข Optimal segmentsโ€ข O(ฮฑ(n)) per queryPerformance ComparisonBrute Force BFS:O(q ร— (n + e)) - SlowUnion-Find:O(q ร— ฮฑ(n)) - OptimalKey Insight:Non-intersecting queries enable efficient tracking
Understanding the Visualization
1
Initial Highway
Cities are connected sequentially: 0โ†’1โ†’2โ†’3โ†’4
2
First Express Highway
Add highway 0โ†’3, bypassing cities 1 and 2
3
Second Express Highway
Add highway 1โ†’4, but optimal path is still 0โ†’3โ†’4
4
Union-Find Optimization
Track bypassed cities efficiently without recalculating entire paths
Key Takeaway
๐ŸŽฏ Key Insight: The non-intersecting property of queries allows us to use Union-Find to efficiently track which cities become unreachable, avoiding the need to recalculate paths from scratch.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.7K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen