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 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:
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
.
Example 1:
Input: n = 3, startPos = [0,1], s = "RRDDLU" Output: [1,5,4,3,1,0] Explanation: Starting from startPos and beginning execution from the ith instruction: - 0th: "RRDDLU". Only one instruction "R" can be executed before it moves off the grid. - 1st: "RDDLU". All five instructions can be executed while it stays in the grid and ends at (1, 1). - 2nd: "DDLU". All four instructions can be executed while it stays in the grid and ends at (1, 0). - 3rd: "DLU". All three instructions can be executed while it stays in the grid and ends at (0, 0). - 4th: "LU". Only one instruction "L" can be executed before it moves off the grid. - 5th: "U". If moving up, it would move off the grid.
Example 2:
Input: n = 2, startPos = [1,1], s = "LURD" Output: [4,1,0,0] Explanation: - 0th: "LURD". - 1st: "URD". - 2nd: "RD". - 3rd: "D".
Example 3:
Input: n = 1, startPos = [0,0], s = "LRUD" Output: [0,0,0,0] Explanation: No matter which instruction the robot begins execution from, it would move off the grid.
Constraints:
m == s.length
1 <= n, m <= 500
startPos.length == 2
0 <= startrow, startcol < n
s
consists of 'L'
, 'R'
, 'U'
, and 'D'
.This problem asks to determine how many instructions a robot can execute from each starting position in a given instruction string, given constraints on staying within a grid. The solution involves simulating the robot's movement for each starting instruction.
Approach:
The core idea is to iterate through each instruction in the input string s
. For each starting instruction index i
, simulate the robot's movement following the suffix instructions (from s[i:]
). The simulation stops when either the robot goes out of bounds or all the instructions are processed. The count of executed instructions for each starting index is then recorded.
Time Complexity Analysis:
The outer loop iterates through the length of the instruction string m
, The inner loop, in the worst-case scenario, iterates through the remaining instructions in the string for each starting index. Thus, the overall time complexity is O(m²), where m is the length of the instruction string.
Space Complexity Analysis:
The space complexity is dominated by the output array ans
which stores the results for each starting index in s
. Therefore, the space complexity is O(m).
Code Explanation (Python):
class Solution:
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
ans = []
m = len(s)
mp = {"L": [0, -1], "R": [0, 1], "U": [-1, 0], "D": [1, 0]} #Direction mapping
for i in range(m): #Iterate through starting instruction indices
x, y = startPos #Robot's starting position
t = 0 #Instruction counter
for j in range(i, m): #Simulate robot movement
a, b = mp[s[j]] #Get direction change from the mapping
if 0 <= x + a < n and 0 <= y + b < n: #Check boundary conditions
x, y, t = x + a, y + b, t + 1 #Update position and counter
else:
break #Robot went out of bounds
ans.append(t) #Record the count
return ans
The Python code effectively implements this approach. The mp
dictionary maps instruction characters ('L', 'R', 'U', 'D') to their corresponding coordinate changes. The nested loops simulate the robot's movements, checking for boundary violations at each step.
Code in other Languages:
The provided code snippets in Java, C++, Go, TypeScript, Rust and C follow the same logic and algorithmic structure as the Python solution. They differ only in syntax and data structure conventions specific to each programming language. The core principle of simulating robot movements and checking boundary conditions remains consistent across all implementations.