Execution of All Suffix Instructions Staying in a Grid
pupil · 430 · lc medium +25 · verified · 82% accepted · 566 likes · top 93%
Description
There is an n x n grid, with the top-left cell at (0, 0) and the bottom-right cell at (n - 1, n - 1). You are given the integer n and an integer array startPos where startPos = [startrow, startcol] indicates that a robot starts at cell (startrow, startcol).
You are also given a 0-indexed string s of length m where s[i] is the ith instruction for the robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down).
The robot can begin executing from any index i in s, executing instructions through the end of s. It halts as soon as:
- The next instruction would move the robot outside the grid boundaries.
- There are no more instructions to execute.
Return an array answer of length m where answer[i] is the number of instructions the robot can execute when starting from the ith instruction.
Example 1:
Example 2:
Example 3:
Code