Minimum Time to Visit Disappearing Nodes - Problem

There is an undirected graph of n nodes. You are given a 2D array edges, where edges[i] = [ui, vi, lengthi] describes an edge between node ui and node vi with a traversal time of lengthi units.

Additionally, you are given an array disappear, where disappear[i] denotes the time when the node i disappears from the graph and you won't be able to visit it.

Note: The graph might be disconnected and might contain multiple edges. Return the array answer, with answer[i] denoting the minimum units of time required to reach node i from node 0. If node i is unreachable from node 0 then answer[i] is -1.

Input & Output

Example 1 — Basic Graph with Disappearing Nodes
$ Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]
Output: [0,2,3]
💡 Note: Start at node 0 (time 0). Path 0→1 takes 2 time units, but node 1 disappears at time 1, so it's unreachable directly. However, path 0→1→2 takes 2+1=3 time units, arriving at node 2 before it disappears at time 5. Direct path 0→2 takes 4 time units, but 0→1→2 is faster at 3 units.
Example 2 — Early Disappearing Node
$ Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]
Output: [0,2,3]
💡 Note: Node 0 disappears at time 1, but we start there at time 0 so it's reachable. Node 1 can be reached via 0→1 in 2 time units (before time 3). Node 2 is reachable via 0→1→2 in 3 time units (before time 5).
Example 3 — Unreachable Node
$ Input: n = 2, edges = [[0,1,10]], disappear = [1,1]
Output: [0,-1]
💡 Note: Node 1 disappears at time 1, but it takes 10 time units to reach it from node 0. Since 10 > 1, node 1 is unreachable, so return -1.

Constraints

  • 1 ≤ n ≤ 5 × 104
  • 0 ≤ edges.length ≤ 105
  • edges[i] = [ui, vi, lengthi]
  • 0 ≤ ui, vi ≤ n - 1
  • 1 ≤ lengthi ≤ 104
  • 1 ≤ disappear[i] ≤ 105

Visualization

Tap to expand
Minimum Time to Visit Disappearing NodesInput Graph012241disappear: ∞disappear: 1disappear: 5Algorithm Process1. Start at node 0 (time 0)2. Path 0→1 takes 2 units3. Node 1 disappears at time 14. Check path 0→2 directlyFinal ResultNode 0: time 0 (start)Node 1: time -1 (unreachable)Node 2: time 4 (direct path)Output: [0,-1,4]Key ConstraintNode i is reachable only if:shortest_path_time < disappear[i]🎯 Key Insight: Modified Dijkstra with time-based node availability
Understanding the Visualization
1
Input Graph
Graph with weighted edges and disappear times for each node
2
Path Finding
Use modified Dijkstra to find shortest paths respecting time constraints
3
Result
Minimum time to reach each node, or -1 if unreachable
Key Takeaway
🎯 Key Insight: This is a shortest path problem with time constraints - use Dijkstra's algorithm but only visit nodes before they disappear
Asked in
Google 25 Amazon 20 Meta 15 Microsoft 12
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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