Execution of All Suffix Instructions Staying in a Grid - Problem

There is an n × 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 is initially 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 ith instruction in s. It executes the instructions one by one towards the end of s but it stops if either of these conditions is met:

  • The next instruction will move the robot off the grid.
  • There are no more instructions left to execute.

Return an array answer of length m where answer[i] is the number of instructions the robot can execute if the robot begins executing from the ith instruction in s.

Input & Output

Example 1 — Basic Grid Navigation
$ Input: n = 3, startPos = [0,1], s = "RRDDLU"
Output: [1,5,4,3,1,0]
💡 Note: Starting from index 0: R→[0,2], next R would go out of bounds = 1 step. Index 1: R→[0,2], D→[1,2], D→[2,2], L→[2,1], U→[1,1] = 5 steps. Index 2: D→[1,1], D→[2,1], L→[2,0], U→[1,0] = 4 steps. Index 3: D→[1,1], L→[1,0], U→[0,0] = 3 steps. Index 4: L→[0,0], next U would go out of bounds = 1 step. Index 5: U would go out of bounds immediately = 0 steps.
Example 2 — Smaller Grid
$ Input: n = 2, startPos = [1,1], s = "LURD"
Output: [4,1,1,1]
💡 Note: From [1,1] in 2x2 grid: Index 0 LURD: L→[1,0], U→[0,0], R→[0,1], D→[1,1] all valid = 4 steps. Index 1 URD: U→[0,1], R→would go to [0,2] out of bounds = 1 step.
Example 3 — Edge Position
$ Input: n = 1, startPos = [0,0], s = "LRUD"
Output: [0,0,0,0]
💡 Note: In 1x1 grid at [0,0], any movement L,R,U,D goes out of bounds immediately, so 0 steps for all starting positions.

Constraints

  • 1 ≤ n ≤ 1000
  • 0 ≤ startPos[0], startPos[1] < n
  • 1 ≤ s.length ≤ 1000
  • s consists of 'L', 'R', 'U', and 'D'

Visualization

Tap to expand
Suffix Instructions in Grid - Optimized Simulation INPUT S 0 1 2 0 1 2 n = 3 startPos = [0, 1] s = "RRDDLU" R R D D L U 0 1 2 3 4 5 ALGORITHM STEPS 1 For each suffix index i Start from startPos 2 Simulate movements L/R/U/D from position 3 Check bounds Stop if out of grid 4 Count valid moves Store in answer[i] Example: i=0 "RRDDLU" R: (0,1)-->(0,2) OK R: (0,2)-->(0,3) OUT! Wait, recalc... R(0,2) D(1,2) D(2,2) L(2,1) L(2,0) U(1,0) = 4 moves answer[0] = 4 FINAL RESULT Moves from each suffix: i=0 "RRDDLU": 4 moves i=1 "RDDLU": 1 move i=2 "DDLU": 3 moves i=3 "DLU": 2 moves i=4 "LU": 0 moves i=5 "U": 0 moves Output: [4,1,3,2,0,0] OK - Verified! Grid shows valid paths within n x n bounds Key Insight: For each suffix starting at index i, simulate the robot's movement from startPos. The early termination occurs when the robot would move outside the [0, n-1] bounds. Count valid moves before stopping. Time: O(m^2) where m is string length. Each suffix requires fresh simulation. TutorialsPoint - Execution of All Suffix Instructions Staying in a Grid | Optimized Simulation with Early Termination
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen