2577. Minimum Time to Visit a Cell In a Grid #891
-
Topics: You are given a You are standing in the top-left cell of the matrix in the 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 Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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. Approach:
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) {
$m = count($grid);
$n = count($grid[0]);
// Early exit if the adjacent cells from the starting point are both blocked
if ($grid[0][1] > 1 && $grid[1][0] > 1) {
return -1;
}
// Directional movements: right, down, left, up
$dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
// Priority queue to store (time, row, col)
$minHeap = new SplPriorityQueue();
$minHeap->setExtractFlags(SplPriorityQueue::EXTR_DATA); // Extract data only
$minHeap->insert([0, 0, 0], 0); // (time, i, j)
// Seen array to track visited cells
$seen = array_fill(0, $m, array_fill(0, $n, false));
$seen[0][0] = true;
while (!$minHeap->isEmpty()) {
list($time, $i, $j) = $minHeap->extract();
if ($i === $m - 1 && $j === $n - 1) {
return $time; // Reached the bottom-right cell
}
foreach ($dirs as $dir) {
$x = $i + $dir[0];
$y = $j + $dir[1];
// Skip invalid or already visited cells
if ($x < 0 || $x >= $m || $y < 0 || $y >= $n || $seen[$x][$y]) {
continue;
}
// Calculate the waiting time if needed
$extraWait = ($grid[$x][$y] - $time) % 2 === 0 ? 1 : 0;
$nextTime = max($time + 1, $grid[$x][$y] + $extraWait);
$minHeap->insert([$nextTime, $x, $y], -$nextTime); // Use -$nextTime to mimic min-heap
$seen[$x][$y] = true;
}
}
return -1; // Unable to reach the bottom-right cell
}
// 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
?> Explanation:
Complexity Analysis
Example RunsInput:$grid = [
[0, 1, 3, 2],
[5, 1, 2, 5],
[4, 3, 8, 6]
];
echo minimumTime($grid); // Output: 7 Input:$grid = [
[0, 2, 4],
[3, 2, 1],
[1, 0, 4]
];
echo minimumTime($grid); // Output: -1 This solution is efficient and works well within the constraints. |
Beta Was this translation helpful? Give feedback.
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.
Approach:
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 ne…