15% off all plans & roadmaps Use code
See plansLeetCode 2577 · Solution & editorial
You are given a m x n matrix grid consisting of non-negative integers where grid[row][col] represents the minimum time required to be able to visit the cell (row, col), which means you can visit the cell (row, col) only when the time you visit it is greater than or equal to grid[row][col].
Minimum Time to Visit a Cell In a Grid is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master Dijkstra's algorithm for finding shortest paths in weighted graphs with non-negative edges.
The reference solution runs in O(m * n * log(m * n)) time and O(m * n) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Minimum Time to Visit a Cell In a Grid is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.