Robot Bounded In Circle - Complete Solution Guide
Robot Bounded In Circle is LeetCode problem 1041, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that: The north direction is the positive direction of the y-axis. The south direction is the negative direction of the y-axis. The east direction is the positive direction of the x-axis. The west direction is the negative direction of the x-axis. The robot can receive one of three instructions: "G" : go straight 1 unit. "L" : turn 90 degrees to the left (i.e., anti-clockwise direction). "R" : turn 90 degrees to the r
Detailed Explanation
The problem asks us to determine whether a robot, starting at (0, 0) and facing North, will remain within a bounded circular area on an infinite plane after repeatedly executing a given sequence of instructions. The robot can move one unit forward ('G'), turn 90 degrees left ('L'), or turn 90 degrees right ('R'). We need to return `true` if the robot stays within a circle, and `false` otherwise. The instructions are repeated infinitely.
Solution Approach
The solution simulates the robot's movement by iterating through the instructions once. It keeps track of the robot's current position (x, y) and direction. After executing the instructions, it checks if either the robot is back at the origin or it is not facing North. If either condition is true, it means the robot is bounded; otherwise, it is not.
Step-by-Step Algorithm
- Step 1: Initialize the robot's position to (0, 0) and its direction to North (represented by 0). Use an array to store displacement vectors for each direction (North, East, South, West).
- Step 2: Iterate through the instructions string.
- Step 3: For each instruction, update the robot's position and direction. If the instruction is 'G', update the x and y coordinates based on the current direction. If the instruction is 'L', turn the robot 90 degrees to the left (decrement direction modulo 4). If the instruction is 'R', turn the robot 90 degrees to the right (increment direction modulo 4).
- Step 4: After processing all instructions, check if the robot has returned to the origin (x == 0 and y == 0) or if the robot is not facing North (direction != 0).
- Step 5: Return `true` if either condition from Step 4 is met; otherwise, return `false`.
Key Insights
- Insight 1: The robot is bounded if it returns to the origin after executing the instructions once.
- Insight 2: The robot is also bounded if, after executing the instructions once, it is facing a direction other than North. If it's facing any other direction, each repetition of the instructions will rotate the direction vector, eventually leading back towards the origin.
- Insight 3: If the robot ends up at a non-origin point facing North, it will continue to move away from the origin indefinitely, and thus is not bounded.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Math, String, Simulation.
Companies
Asked at: Airbnb, Chewy, Nvidia, ZScaler.