Watch 9 video solutions for Minimum Time to Visit Disappearing Nodes, a medium level problem involving Array, Graph, Heap (Priority Queue). This walkthrough by Aryan Mittal has 2,732 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 that 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.
Example 1:
Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]
Output: [0,-1,4]
Explanation:

We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.
edges[0]. Unfortunately, it disappears at that moment, so we won't be able to visit it.edges[2].Example 2:
Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]
Output: [0,2,3]
Explanation:

We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.
edges[0].edges[0] and edges[1].Example 3:
Input: n = 2, edges = [[0,1,1]], disappear = [1,1]
Output: [0,-1]
Explanation:
Exactly when we reach node 1, it disappears.
Constraints:
1 <= n <= 5 * 1040 <= edges.length <= 105edges[i] == [ui, vi, lengthi]0 <= ui, vi <= n - 11 <= lengthi <= 105disappear.length == n1 <= disappear[i] <= 105Problem Overview: You are given a weighted graph and an array disappear where each node becomes inaccessible after a specific time. Starting from node 0, compute the minimum time to reach every node before it disappears. If a node cannot be reached in time, its answer is -1.
Approach 1: Dijkstra's Algorithm with Disappearance Constraint (O((n+m) log n) time, O(n+m) space)
This problem behaves like a classic shortest path search with one extra rule: you cannot enter a node if the arrival time is greater than or equal to its disappearance time. Use Dijkstra’s algorithm with a heap (priority queue) to always process the node with the smallest current travel time. For each edge relaxation, compute next_time = current_time + weight and only push the neighbor if next_time < disappear[neighbor]. Maintain a distance array to avoid revisiting nodes with worse times. This approach works well because edge weights vary, and the priority queue guarantees the earliest feasible arrival is processed first.
Approach 2: Breadth-First Search with Edge Constraints (O(n+m) time, O(n+m) space)
If edge traversal behaves close to uniform or you structure the traversal level-by-level, a constrained BFS-style traversal can work. Store states in a queue and track the earliest time each node can be reached. When exploring neighbors, calculate the arrival time and skip the transition if the node would already have disappeared. This method still relies on pruning invalid states early and maintaining a visited or distance array to avoid redundant processing. Conceptually it treats the graph as layers of reachable states while enforcing the disappearance rule during expansion. It is useful for simpler implementations when edge weights or constraints allow near-linear traversal.
Recommended for interviews: Dijkstra’s algorithm is the expected solution. Interviewers want to see that you recognize the problem as a constrained graph shortest-path variant and modify the relaxation condition using the disappearance time. A brute traversal shows understanding of graph exploration, but the priority queue solution demonstrates mastery of shortest-path optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dijkstra's Algorithm with Disappearance Constraint | O((n+m) log n) | O(n+m) | General weighted graph case where edges have different travel times |
| Breadth-First Search with Edge Constraints | O(n+m) | O(n+m) | When traversal behaves close to uniform or constraints allow simple queue-based exploration |