Tap here to switch tabs
Problem
Submissions

Problem: Execution of All Suffix Instructions Staying in a Grid

med
30 min
Try to solve the Execution of All Suffix Instructions Staying in a Grid problem.

Statement

There is an nimesnn imes n grid with coordinates ranging from (0,0)(0, 0) to (n−1,n−1)(n - 1, n - 1). A robot is placed at the starting position startPos =[startrow,startcol]= [start_{row}, start_{col}].

You are given the integer n, the array startPos, and a string s of length mm representing movement instructions. The valid directions are:

  • 'L': Move left by decreasing the column index by 11.

  • 'R': Move right by increasing the column index by 11.

  • 'U': Move up by decreasing the row index by 11.

  • 'D': Move down by increasing the row index by 11.

For each starting instruction index i from 00 to m−1m - 1, the robot begins at startPos and executes the instructions in s sequentially starting from index i. The execution stops as soon as a move would take the robot off the grid or all remaining instructions are completed.

Return an array of length mm where the iextthi^{ ext{th}} element is the number of instructions the robot can execute starting from index i.

Constraints:

  • m ==== s.length

  • 1≤1 \leq n, m ≤500\leq 500

  • startPos.length ==2== 2

  • 0≤startrow,startcol<0 \leq start_{row}, start_{col} < n

  • s consists of 'L', 'R', 'U', and 'D'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Execution of All Suffix Instructions Staying in a Grid

med
30 min
Try to solve the Execution of All Suffix Instructions Staying in a Grid problem.

Statement

There is an nimesnn imes n grid with coordinates ranging from (0,0)(0, 0) to (n−1,n−1)(n - 1, n - 1). A robot is placed at the starting position startPos =[startrow,startcol]= [start_{row}, start_{col}].

You are given the integer n, the array startPos, and a string s of length mm representing movement instructions. The valid directions are:

  • 'L': Move left by decreasing the column index by 11.

  • 'R': Move right by increasing the column index by 11.

  • 'U': Move up by decreasing the row index by 11.

  • 'D': Move down by increasing the row index by 11.

For each starting instruction index i from 00 to m−1m - 1, the robot begins at startPos and executes the instructions in s sequentially starting from index i. The execution stops as soon as a move would take the robot off the grid or all remaining instructions are completed.

Return an array of length mm where the iextthi^{ ext{th}} element is the number of instructions the robot can execute starting from index i.

Constraints:

  • m ==== s.length

  • 1≤1 \leq n, m ≤500\leq 500

  • startPos.length ==2== 2

  • 0≤startrow,startcol<0 \leq start_{row}, start_{col} < n

  • s consists of 'L', 'R', 'U', and 'D'.