class Solution:
def isRobotBounded(self, instructions: str) -> bool:
# Directions: North, East, South, West
directions = [(0,1), (1,0), (0,-1), (-1,0)]
x, y = 0, 0
dir_idx = 0 # Start facing North
for ch in instructions:
if ch == 'G':
dx, dy = directions[dir_idx]
x += dx
y += dy
elif ch == 'L':
dir_idx = (dir_idx + 3) % 4 # Turn left
else: # ch == 'R'
dir_idx = (dir_idx + 1) % 4 # Turn right
# If back at the origin or not facing North, it's bounded
return (x == 0 and y == 0) or dir_idx != 0
class Solution {
public:
bool isRobotBounded(string instructions) {
// Directions: North, East, South, West
vector<pair<int,int>> directions = {{0,1}, {1,0}, {0,-1}, {-1,0}};
int x = 0, y = 0, dir = 0; // Start at (0,0) facing North
for (char ch : instructions) {
if (ch == 'G') {
x += directions[dir].first;
y += directions[dir].second;
} else if (ch == 'L') {
dir = (dir + 3) % 4; // Turn left
} else { // 'R'
dir = (dir + 1) % 4; // Turn right
}
}
// Bounded if back at origin or not facing North
return (x == 0 && y == 0) || dir != 0;
}
};
class Solution {
public boolean isRobotBounded(String instructions) {
// Directions: North, East, South, West
int[][] directions = {{0,1}, {1,0}, {0,-1}, {-1,0}};
int x = 0, y = 0, dir = 0; // Start at (0,0) facing North
for (char ch : instructions.toCharArray()) {
if (ch == 'G') {
x += directions[dir][0];
y += directions[dir][1];
} else if (ch == 'L') {
dir = (dir + 3) % 4; // Turn left
} else { // 'R'
dir = (dir + 1) % 4; // Turn right
}
}
// Bounded if back at origin or not facing North
return (x == 0 && y == 0) || dir != 0;
}
}
var isRobotBounded = function(instructions) {
// Directions: North, East, South, West
const directions = [[0,1], [1,0], [0,-1], [-1,0]];
let x = 0, y = 0, dir = 0; // Start at (0,0) facing North
for (const ch of instructions) {
if (ch === 'G') {
x += directions[dir][0];
y += directions[dir][1];
} else if (ch === 'L') {
dir = (dir + 3) % 4; // Turn left
} else { // 'R'
dir = (dir + 1) % 4; // Turn right
}
}
// Bounded if back at origin or not facing North
return (x === 0 && y === 0) || dir !== 0;
};
Given a string instructions
consisting only of the characters 'G'
(go straight), 'L'
(turn left 90 degrees), and 'R'
(turn right 90 degrees), a robot starts at the origin (0, 0) facing north. It executes the sequence of instructions repeatedly and infinitely. The task is to determine whether the robot stays within a circle (i.e., is bounded in a circle) or whether it will wander off to infinity.
Key constraints:
true
if the robot is bounded in a circle, otherwise false
.At first glance, it might seem like we need to simulate the robot's movement for a very long time or even infinitely to see if it stays within a circle. However, that would be too slow and inefficient. Instead, we should look for patterns or invariants that allow us to determine boundedness after just one pass of the instructions.
Let's consider what can happen after one full iteration of the instruction string:
Here’s how we solve the problem step-by-step:
x
and y
to represent the robot's current coordinates.dir
to represent the robot's current direction: 0 for north, 1 for east, 2 for south, 3 for west.instructions
:x == 0 && y == 0
), it is bounded.dir != 0
), it is also bounded because repeating the instructions will eventually return it to the origin or form a cycle.This approach is efficient and only requires a single pass through the instruction string.
Let's walk through an example with instructions = "GLR"
:
After one pass, the robot is at position (0,1), facing north. Since it's not at the origin and still facing north, repeating the instructions will move it further away (to (0,2), (0,3), etc.), so it is not bounded.
Now consider instructions = "GL"
:
After one pass, the robot is at (0,1), facing west. Since it's not facing north, repeating the instructions will make the robot trace a square, eventually returning to the origin. So it is bounded.
Brute-force approach: If we tried to simulate the robot's movement for many cycles, the time complexity would be O(N * K), where N is the instruction length and K is the number of cycles simulated (potentially very large or infinite).
Optimized approach (our solution):
instructions
, because we only loop through the instructions once.The key insight for this problem is that you only need to simulate the instructions once and check the robot's position and orientation. If the robot is back at the origin or not facing north, it will stay bounded in a circle. This leads to an elegant O(N) solution that avoids unnecessary repeated simulation and leverages the cyclical nature of the robot's possible states.