Skip to content

Commit 08b5b4e

Browse files
authored
Create robot-bounded-in-circle.py
1 parent fee17cb commit 08b5b4e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/robot-bounded-in-circle.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def isRobotBounded(self, instructions):
6+
"""
7+
:type instructions: str
8+
:rtype: bool
9+
"""
10+
directions = [[ 1, 0], [0, -1], [-1, 0], [0, 1]]
11+
x, y, i = 0, 0, 0
12+
for instruction in instructions:
13+
if instruction == 'R':
14+
i = (i+1) % 4;
15+
elif instruction == 'L':
16+
i = (i-1) % 4;
17+
else:
18+
x += directions[i][0]
19+
y += directions[i][1]
20+
return (x == 0 and y == 0) or i > 0

0 commit comments

Comments
 (0)