Last Visited Cell in a Directional Grid Problem
Description
LeetCode Problem 4084.
You are given an m x n matrix grid where each cell contains one of the following characters:
'L': move one cell left.'R': move one cell right.'U': move one cell up.'D': move one cell down.
You start at cell (0, 0), which is considered visited. At each step, move to the cell indicated by the character in your current cell.
The process ends if the indicated move would either leave the grid or enter a cell that has already been visited. In either case, the move is not made.
Return an integer array [r, c] representing the row and column indices of the last visited cell. The indices are 0-based.
Example 1:
Input: grid = [["R","D"],["U","L"]]
Output: [1,0]
Explanation:
- Start at cell (0, 0), which contains 'R', so move right to (0, 1).
- At cell (0, 1), the character is 'D', so move down to (1, 1).
- At cell (1, 1), the character is 'L', so move left to (1, 0).
- At cell (1, 0), the character is 'U', which indicates moving to (0, 0). However, (0, 0) has already been visited, so this move is not made.
- Therefore, the process ends at the last visited cell (1, 0), so the answer is [1, 0].
Example 2:
Input: grid = [["R","R","R"]]
Output: [0,2]
Explanation:
- Start at cell (0, 0), which contains 'R', so move right to (0, 1).
- At cell (0, 1), the character is 'R', so move right to (0, 2).
- At cell (0, 2), the character is 'R', which indicates moving to (0, 3). However, (0, 3) is outside the grid, so this move is not made.
- Therefore, the process ends at the last visited cell (0, 2), so the answer is [0, 2].
Constraints:
1 <= m == grid.length <= 10^51 <= n == grid[i].length <= 10^5m * n <= 10^5grid[i][j]is one of'L','R','U', or'D'.
Solution
Approach: Simulation
Follow the arrows from (0, 0) and keep a visited table. At each cell compute the cell the arrow points to. If it is inside the grid and not yet visited, move there and mark it; otherwise the process ends and the current cell is the answer. Every cell is entered at most once, so the walk has at most m * n steps.
Complexity: O(m * n) time and O(m * n) space.
class Solution:
def lastVisitedCell(self, grid: List[List[str]]) -> List[int]:
m, n = len(grid), len(grid[0])
dirs = {"L": (0, -1), "R": (0, 1), "U": (-1, 0), "D": (1, 0)}
r = c = 0
vis = [[False] * n for _ in range(m)]
while True:
vis[r][c] = True
dr, dc = dirs[grid[r][c]]
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n and not vis[nr][nc]:
r, c = nr, nc
else:
return [r, c]