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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code