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,-1,4]
💡 Note: Start at node 0 (time 0). Node 1 is unreachable because the path 0→1 takes 2 time units but node 1 disappears at time 1. Node 2 can be reached directly via 0→2 in 4 time units, arriving before it disappears at time 5. The path 0→1→2 is not possible since node 1 is unreachable.
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 Nodes INPUT 0 1 2 2 1 4 n = 3 edges = [[0,1,2], [1,2,1],[0,2,4]] disappear = [1, 1, 5] dis:1 dis:1 dis:5 Graph with edge weights and disappear times ALGORITHM STEPS 1 Initialize dist[0]=0, others=INF 2 Min-Heap Push Push (0, node0) to heap 3 Process Node Check time < disappear[i] 4 Relax Edges Update if new_time < dist Dijkstra Processing: Pop(0,0): Visit node 0 0-->1: t=2 > dis[1]=1 SKIP 0-->2: t=4, but via 1-->2 Pop(2,1): t=2, relax 1-->2 dist[2]=2+1=3 < dis[2]=5 OK Time constraint validation FINAL RESULT 0 t=0 1 t=2 2 t=3 Output: [0, 2, 3] Node 0: Start (time 0) Node 1: 0-->1 (time 2) Node 2: 0-->1-->2 (time 3) All within disappear times! Key Insight: Modified Dijkstra's algorithm with time constraint: Only visit node i if arrival_time < disappear[i]. Skip relaxation for neighbors that have already disappeared. This ensures we find shortest paths while respecting temporal constraints. Time Complexity: O((V+E) log V) using min-heap. TutorialsPoint - Minimum Time to Visit Disappearing Nodes | Dijkstra with Time Constraints
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