Welcome to Subscribe On Youtube
4084. Last Visited Cell in a Directional Grid
Description
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 <= 1051 <= n == grid[i].length <= 105m * n <= 105grid[i][j]is one of'L','R','U', or'D'.
Solutions
Solution 1: Simulation
Thinking
The grid has at most $10^5$ cells. Following the arrows without remembering visited cells never stops once the path enters a cycle.
Movement ends in only two cases: the next step would leave the grid, or it would enter a cell that was already visited. In both cases that step is not taken, and the answer is the previous valid coordinate.
Each cell is entered at most once. Mark visits in a two-dimensional boolean array, start at $(0,0)$, update the row and column from the current character, and return the saved coordinate when the next step is illegal.
Simulate from $(0,0)$. Use a boolean array $\textit{vis}$ to mark cells that have already been visited. At each step, save the current coordinate $(r,c)$, then move according to $\textit{grid}[r][c]$: $L$ and $R$ change the column, while $U$ and $D$ change the row. If the new coordinate is outside the grid or its cell has already been visited, do not take that step and return the saved coordinate. Each cell is visited at most once.
The time complexity is $O(mn)$, and the space complexity is $O(mn)$, where $m$ and $n$ are the number of rows and columns.
-
class Solution { public int[] lastVisitedCell(char[][] grid) { int m = grid.length, n = grid[0].length; int r = 0, c = 0; boolean[][] vis = new boolean[m][n]; while (true) { int[] cur = {r, c}; vis[r][c] = true; switch (grid[r][c]) { case 'L' -> c--; case 'R' -> c++; case 'U' -> r--; case 'D' -> r++; } if (r >= 0 && r < m && c >= 0 && c < n && !vis[r][c]) { continue; } return cur; } } } -
class Solution { public: vector<int> lastVisitedCell(vector<vector<char>>& grid) { int m = grid.size(), n = grid[0].size(); int r = 0, c = 0; vector<vector<bool>> vis(m, vector<bool>(n)); while (true) { vector<int> cur = {r, c}; vis[r][c] = true; switch (grid[r][c]) { case 'L': c--; break; case 'R': c++; break; case 'U': r--; break; case 'D': r++; break; } if (r >= 0 && r < m && c >= 0 && c < n && !vis[r][c]) { continue; } return cur; } } }; -
class Solution: def lastVisitedCell(self, grid: list[list[str]]) -> list[int]: m, n = len(grid), len(grid[0]) r = c = 0 vis = [[False] * n for _ in range(m)] while 1: cur = [r, c] vis[r][c] = True match grid[r][c]: case "L": c -= 1 case "R": c += 1 case "U": r -= 1 case "D": r += 1 if 0 <= r < m and 0 <= c < n and not vis[r][c]: continue return cur -
func lastVisitedCell(grid [][]byte) []int { m, n := len(grid), len(grid[0]) r, c := 0, 0 vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) } for { cur := []int{r, c} vis[r][c] = true switch grid[r][c] { case 'L': c-- case 'R': c++ case 'U': r-- case 'D': r++ } if r >= 0 && r < m && c >= 0 && c < n && !vis[r][c] { continue } return cur } } -
function lastVisitedCell(grid: string[][]): number[] { const m = grid.length, n = grid[0].length; let r = 0, c = 0; const vis = Array.from({ length: m }, () => Array(n).fill(false)); while (true) { const cur = [r, c]; vis[r][c] = true; switch (grid[r][c]) { case 'L': c--; break; case 'R': c++; break; case 'U': r--; break; case 'D': r++; break; } if (r >= 0 && r < m && c >= 0 && c < n && !vis[r][c]) { continue; } return cur; } }