Similar Problems
Similar Problems not available
Minimum Time To Visit A Cell In A Grid - Leetcode Solution
Companies:
LeetCode: Minimum Time To Visit A Cell In A Grid Leetcode Solution
Difficulty: Hard
Topics: breadth-first-search matrix heap-priority-queue array graph
Problem
Given a grid of size m x n, you have to start from the top-left corner and reach the bottom-right corner with a given speed. The grid has empty cells (0) and obstacles (1). You can only travel through empty cells, and it takes 1 unit of time to travel through one cell. However, if you step on a cell with an obstacle, you lose 1 unit of time. Find the minimum time required to reach the bottom-right corner. You can start and end anywhere on the grid.
Example:
Input: grid = [ [0,0,0], [0,1,0], [0,0,0] ], speed = 1
Output: 2
Explanation: Starting at the top-left corner, the minimum time to reach the bottom-right corner is 2. You can either go right -> down -> down or down -> right -> right.
Solution
To solve this problem, we can use the BFS algorithm. We need to form a queue that will help us to carry out the BFS traversal. We will also use a 2D array to keep track of the minimum time required to reach the particular cell. Initially, we will mark all the cells as -1, which represents that the cell is not yet visited. We can use 0 to represent the starting cell. We will also set the minimum time required to reach the starting cell as 0.
For each cell, we will check its neighboring cells. If the neighboring cell is valid and not yet visited, we will add it to the queue and update its minimum time required to reach it. If the neighboring cell is blocked, we will subtract the time penalty (1 unit) from the current cell and update the minimum time required to reach it.
We keep on iterating until we visit the bottom-right corner of the grid. At the end, the cell at the bottom-right corner will have the minimum time required to reach it.
Python Implementation:
from collections import deque
class Solution:
def minTime(self, grid: List[List[int]], speed: int) -> int:
m, n = len(grid), len(grid[0])
queue = deque([(0, 0)])
times = [[-1] * n for _ in range(m)]
times[0][0] = 0 # Start time is 0
while queue:
x, y = queue.popleft()
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
nx, ny = x + dx, y + dy
if nx < 0 or nx >= m or ny < 0 or ny >= n:
continue # Invalid position
# Calculate time required to reach the next cell
# with/without penalty
time = times[x][y] + (0 if grid[nx][ny] == 0 else -1)
if times[nx][ny] == -1 or times[nx][ny] > time:
times[nx][ny] = time
queue.append((nx, ny))
# Return the time required to reach the final cell
return -times[-1][-1] // speed
Time Complexity:
The time complexity of the above algorithm is O(mn), where m and n are the dimensions of the grid. We visit each cell at most once. The time penalty of -1 may cause us to visit some cells multiple times, but the overall complexity is still O(mn).
Space Complexity:
The space complexity of the above algorithm is O(mn) as we are using a 2D array to keep track of the minimum time required to reach each cell. We are also using a queue to store the cells to be visited, which can have at most m * n elements in it.
Minimum Time To Visit A Cell In A Grid Solution Code
1