3389. Minimum Time to Visit Disappearing Nodes
My accepted Python solution to LeetCode problem 3389, Minimum Time to Visit Disappearing Nodes, running in 394ms.
- Difficulty: Medium
- Python
- Runtime 394ms
- Memory 69.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 394ms, memory 69.6MB, accepted 2025-12-31.
class Solution:
def minimumTime(self, n: int, edges: List[List[int]], disappear: List[int]) -> List[int]:
import heapq
from collections import defaultdict
# Build adjacency list
graph = defaultdict(list)
for u, v, length in edges:
graph[u].append((v, length))
graph[v].append((u, length))
# Dijkstra's algorithm
answer = [-1] * n
answer[0] = 0
pq = [(0, 0)] # (time, node)
while pq:
time, node = heapq.heappop(pq)
if time > answer[node] and answer[node] != -1:
continue
for neighbor, length in graph[node]:
new_time = time + length
# Can only visit if we arrive before it disappears
if new_time < disappear[neighbor]:
if answer[neighbor] == -1 or new_time < answer[neighbor]:
answer[neighbor] = new_time
heapq.heappush(pq, (new_time, neighbor))
return answer