LeetCode Problem

How to Solve Minimum Time to Visit a Cell In a Grid

Use a priority queue combined with breadth-first search to explore the grid efficiently. Track the earliest time each cell can be visited and always expand the next reachable cell with the smallest current time. If the bottom-right cell is never reached under these constraints, return -1 to indicate it is impossible.

GhostInterview Help

Need help with Minimum Time to Visit a Cell In a Grid without spending extra time grinding it?

GhostInterview can read Minimum Time to Visit a Cell In a Grid from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2577Graph traversal with breadth-first searchReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Graph traversal with breadth-first search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Use a priority queue combined with breadth-first search to explore the grid efficiently. Track the earliest time each cell can be visited and always expand the next reachable cell with the smallest current time. If the bottom-right cell is never reached under these constraints, return -1 to indicate it is impossible.

Problem Statement

You are given an m x n integer matrix grid where grid[row][col] represents the minimum time required to enter cell (row, col). You start at the top-left cell at time 0 and can move in four directions: up, down, left, and right, each taking 1 second per move.

Determine the minimum time to reach the bottom-right cell following the cell time constraints. If no path exists that satisfies all cell requirements, return -1.

Examples

Example 1

Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]

Output: 7

One of the paths that we can take is the following:

  • at t = 0, we are on the cell (0,0).
  • at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.
  • at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.
  • at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.
  • at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.
  • at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.
  • at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.
  • at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7. The final time is 7. It can be shown that it is the minimum time possible.

Example 2

Input: grid = [[0,2,4],[3,2,1],[1,0,4]]

Output: -1

There is no path from the top left to the bottom-right cell.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 2 <= m, n <= 1000
  • 4 <= m * n <= 105
  • 0 <= grid[i][j] <= 105
  • grid[0][0] == 0

Solution Approach

Use BFS with a Priority Queue

Implement a priority queue to always expand the cell with the earliest reachable time. Push the starting cell with time 0, then for each move, calculate the next valid time considering the grid's constraints and push it into the queue.

Track Visited Cells Efficiently

Maintain a 2D array to record the earliest time each cell can be reached. Skip processing a cell if the current time is greater than its recorded time. This prevents revisiting cells unnecessarily and ensures the BFS explores optimally.

Handle Impossible Paths

If the priority queue is empty and the bottom-right cell has not been visited, return -1. This handles cases where grid constraints prevent any valid path from reaching the target cell.

Complexity Analysis

MetricValue
TimeO(m \cdot n \log(m \cdot n))
SpaceO(m \cdot n)

The algorithm uses O(m * n log(m * n)) time due to the priority queue operations on each of the m * n cells. Space complexity is O(m * n) for tracking visited times and queue storage.

What Interviewers Usually Probe

  • Focus on BFS and shortest path logic to optimize cell visit times.
  • Consider how cell constraints may block standard BFS paths and require a priority-based expansion.
  • Expect questions about handling edge cases where the target is unreachable.

Common Pitfalls or Variants

Common pitfalls

  • Not accounting for the minimum required time per cell can lead to invalid paths.
  • Using standard BFS without prioritizing earliest arrival time may miss optimal solutions.
  • Failing to track visited times correctly can cause unnecessary reprocessing and wrong results.

Follow-up variants

  • Modify the grid so certain cells have negative delays to test early arrival handling.
  • Use a hexagonal grid with six neighbors instead of four to adjust BFS logic.
  • Change the cost of moving between cells to non-uniform values to test weighted shortest paths.

How GhostInterview Helps

  • GhostInterview provides the step-by-step BFS expansion order to quickly identify valid paths.
  • It tracks cell visit times automatically to show how priority queues prevent incorrect revisits.
  • The solver simulates impossible paths to help visualize why -1 is returned for blocked targets.

Topic Pages

FAQ

What is the main pattern used in Minimum Time to Visit a Cell In a Grid?

The problem uses graph traversal with breadth-first search combined with a priority queue to handle time constraints.

Can I use Dijkstra instead of BFS for this problem?

Yes, using Dijkstra's algorithm is equivalent since each move has a cost of 1 and the priority queue ensures the earliest arrival is expanded first.

Why might the answer be -1 in some grids?

If the constraints of grid cells prevent reaching the bottom-right cell at any valid time, the algorithm correctly returns -1.

What data structures are essential for this problem?

A priority queue for BFS expansion and a 2D array to track earliest reachable times per cell are essential.

How do I handle large grids efficiently?

Process cells using a priority queue and skip revisits using the tracked earliest time to maintain O(m * n log(m * n)) efficiency.

GhostInterview Solver

Need direct help with Minimum Time to Visit a Cell In a Grid instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Time to Visit a Cell In a Grid from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.