Talentd
FleetCode

4084. Last Visited Cell in a Directional Grid

Medium66.9% acceptance

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 <= 105
  • 1 <= n == grid[i].length <= 105
  • m * n <= 105
  • grid[i][j] is one of 'L', 'R', 'U', or 'D'.

Hints open one at a time

Hint 1
Follow the directions while recording visited cells. You can use a separate visited array or mark cells in place after reading their directions.
Hint 2
Calculate the next position before moving. If it is outside the grid or already visited, return the current position.
14:13
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def lastVisitedCell(self, grid: list[list[str]]) -> list[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
[["R","D"],["U","L"]]
Input
[["R","R","R"]]
Your code is saved on this device.Solution