Execution of All Suffix Instructions Staying in a Grid - Problem

Imagine you have a robot on an n ร— n grid that needs to follow a sequence of movement instructions. The grid has coordinates from (0, 0) at the top-left to (n-1, n-1) at the bottom-right.

You're given:

  • The grid size n
  • A starting position startPos = [startRow, startCol]
  • A string s of movement instructions: 'L' (left), 'R' (right), 'U' (up), 'D' (down)

Here's the interesting part: the robot can start executing from any position in the instruction string. For each possible starting position i, you need to determine how many instructions the robot can execute before either:

  1. The next instruction would move it off the grid, or
  2. There are no more instructions to execute

Goal: Return an array where answer[i] is the number of instructions the robot can execute starting from instruction i.

Input & Output

example_1.py โ€” Basic Grid Navigation
$ Input: n = 3, startPos = [0,1], s = "RRDDLU"
โ€บ Output: [1,5,4,3,1,0]
๐Ÿ’ก Note: Starting from index 0: Robot can only execute R (moves to [0,2]), then next R would go out of bounds. Starting from index 1: Robot executes RDDLU (5 moves total). Each starting position has different constraints based on remaining instructions.
example_2.py โ€” Small Grid
$ Input: n = 2, startPos = [1,1], s = "LURD"
โ€บ Output: [4,1,1,1]
๐Ÿ’ก Note: From index 0: All 4 instructions can be executed (LURD brings robot back to [1,1]). From other indices, only 1 instruction can be executed before going out of bounds.
example_3.py โ€” Edge Case
$ Input: n = 1, startPos = [0,0], s = "LRUD"
โ€บ Output: [0,0,0,0]
๐Ÿ’ก Note: In a 1ร—1 grid, any movement instruction takes the robot out of bounds, so 0 instructions can be executed from any starting index.

Constraints

  • m == s.length
  • 1 โ‰ค n, m โ‰ค 500
  • 0 โ‰ค startPos[0], startPos[1] < n
  • s consists of 'L', 'R', 'U', and 'D' only

Visualization

Tap to expand
Robot Grid NavigationRInstructions: "RRD"Start at (1,1) in 3ร—3 gridIndex 0: Rโ†’Rโ†’D = 2 steps(Third R would go out of bounds)Index 1: Rโ†’D = 2 stepsIndex 2: D = 1 stepOut of bounds!
Understanding the Visualization
1
Place Robot
Position the robot at the starting coordinates on the grid
2
Select Starting Instruction
Choose which instruction in the sequence to begin execution from
3
Execute Instructions
Move the robot step by step following each instruction
4
Check Boundaries
Stop if the next instruction would move the robot off the grid
5
Count Valid Moves
Record the total number of instructions successfully executed
Key Takeaway
๐ŸŽฏ Key Insight: Each starting position creates a different suffix of instructions, requiring independent simulation to count valid moves before hitting grid boundaries.
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 15
28.4K Views
Medium Frequency
~15 min Avg. Time
892 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