Robot Bounded In Circle — LeetCode 1041 Python Solution
MediumMathStringSimulation
- Problem
- #1041
- Pattern
- Math and Number Theory
- Reading time
- 3 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- instructions = "GGLLGG"
- Output
- true
- Explanation
- The robot is initially at (0, 0) facing the north direction.
Python solution
Python
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
k = 0
dist = [0] * 4
for c in instructions:
if c == 'L':
k = (k + 1) % 4
elif c == 'R':
k = (k + 3) % 4
else:
dist[k] += 1
return (dist[0] == dist[2] and dist[1] == dist[3]) or k != 0Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1), where n is the length of the instruction string \textit{instructions} auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1041. Robot Bounded In Circle is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1041. Robot Bounded In Circle?
- LeetCode 1041. Robot Bounded In Circle is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1041. Robot Bounded In Circle?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1041. Robot Bounded In Circle?
- The Python solution on this page uses O(1), where n is the length of the instruction string \textit{instructions} auxiliary space.
- What topics does LeetCode 1041. Robot Bounded In Circle cover?
- LeetCode 1041. Robot Bounded In Circle is tagged Math, String and Simulation on LeetCode.