Home >Backend Development >PHP Tutorial >Minimum Time to Visit a Cell In a Grid
2577. Minimum Time to Visit a Cell In a Grid
Difficulty: Hard
Topics: Array, Breadth-First Search, Graph, Heap (Priority Queue), Matrix, Shortest Path
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].
You are standing in the top-left cell of the matrix in the 0th second, and you must move to any adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.
Return the minimum time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the bottom-right cell, then return -1.
Example 1:
Example 2:
Constraints:
Hint:
Solution:
We can apply a modified version of Dijkstra's algorithm using a priority queue. This problem essentially asks us to find the shortest time required to visit the bottom-right cell from the top-left cell, where each move has a time constraint based on the values in the grid.
Graph Representation: Treat each cell in the grid as a node. The edges are the adjacent cells (up, down, left, right) that you can move to.
Priority Queue (Min-Heap): Use a priority queue to always explore the cell with the least time required. This ensures that we are processing the cells in order of the earliest time we can reach them.
Modified BFS: For each cell, we will check if we can move to its neighboring cells and update the time at which we can visit them. If a cell is visited at a later time than the current, we add it back into the queue with the new time.
Early Exit: Once we reach the bottom-right cell, we can return the time. If we exhaust the queue without reaching it, return -1.
Let's implement this solution in PHP: 2577. Minimum Time to Visit a Cell In a Grid
<?php /** * @param Integer[][] $grid * @return Integer */ function minimumTime($grid) { ... ... ... /** * go to ./solution.php */ } // Example 1 $grid1 = [ [0, 1, 3, 2], [5, 1, 2, 5], [4, 3, 8, 6] ]; echo minimumTime($grid1) . PHP_EOL; // Output: 7 // Example 2 $grid2 = [ [0, 2, 4], [3, 2, 1], [1, 0, 4] ]; echo minimumTime($grid2) . PHP_EOL; // Output: -1 ?>
Priority Queue:
The SplPriorityQueue is used to ensure that the cells with the minimum time are processed first. The priority is stored as -time because PHP's SplPriorityQueue is a max-heap by default.
Traversal:
Starting from the top-left cell (0, 0), the algorithm processes all reachable cells, considering the earliest time each can be visited (max(0, grid[newRow][newCol] - (time 1))).
Visited Cells:
A visited array keeps track of cells that have already been processed to avoid redundant computations and infinite loops.
Boundary and Validity Check:
The algorithm ensures we stay within the bounds of the grid and processes only valid neighbors.
Time Calculation:
Each move takes one second, and if the cell requires waiting (i.e., grid[newRow][newCol] > time 1), the algorithm waits until the required time.
Edge Case:
If the queue is exhausted and the bottom-right cell is not reached, the function returns -1.
Time Complexity:
Space Complexity:
<?php /** * @param Integer[][] $grid * @return Integer */ function minimumTime($grid) { ... ... ... /** * go to ./solution.php */ } // Example 1 $grid1 = [ [0, 1, 3, 2], [5, 1, 2, 5], [4, 3, 8, 6] ]; echo minimumTime($grid1) . PHP_EOL; // Output: 7 // Example 2 $grid2 = [ [0, 2, 4], [3, 2, 1], [1, 0, 4] ]; echo minimumTime($grid2) . PHP_EOL; // Output: -1 ?>
$grid = [ [0, 1, 3, 2], [5, 1, 2, 5], [4, 3, 8, 6] ]; echo minimumTime($grid); // Output: 7
This solution is efficient and works well within the constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Minimum Time to Visit a Cell In a Grid. For more information, please follow other related articles on the PHP Chinese website!