Skip to content

Commit 5f9dd46

Browse files
authored
Create 1041_Robot_Bounded_In_Circle.md
1 parent 82b75d8 commit 5f9dd46

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

1041_Robot_Bounded_In_Circle.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 1041. Robot Bounded In Circle
2+
3+
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
4+
5+
"G": go straight 1 unit;
6+
"L": turn 90 degrees to the left;
7+
"R": turn 90 degrees to the right.
8+
The robot performs the instructions given in order, and repeats them forever.
9+
10+
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
11+
12+
13+
14+
Example 1:
15+
16+
Input: instructions = "GGLLGG"
17+
Output: true
18+
Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
19+
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
20+
Example 2:
21+
22+
Input: instructions = "GG"
23+
Output: false
24+
Explanation: The robot moves north indefinitely.
25+
Example 3:
26+
27+
Input: instructions = "GL"
28+
Output: true
29+
Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
30+
31+
32+
Constraints:
33+
34+
1 <= instructions.length <= 100
35+
instructions[i] is 'G', 'L' or, 'R'.
36+
37+
38+
```python
39+
def isRobotBounded(self, instructions: str) -> bool:
40+
41+
dirs = [(1,0), (0,-1), (-1, 0), (0,1) ]
42+
size = 4
43+
facing = 0
44+
r = 0
45+
c = 0
46+
47+
for i in instructions:
48+
print(facing)
49+
if i == 'G':
50+
r = r + dirs[facing][0]
51+
c = c + dirs[facing][1]
52+
53+
elif i == 'L':
54+
facing = (facing + 1)%size
55+
else:
56+
facing = (facing - 1)%size
57+
58+
if r ==0 and c ==0:
59+
return True
60+
61+
if facing !=0:
62+
return True
63+
64+
return False
65+
```
66+
67+
68+
```
69+
Runtime: 32 ms, faster than 60.80% of Python3 online submissions for Robot Bounded In Circle.
70+
Memory Usage: 14.7 MB, less than 19.72% of Python3 online submissions for Robot Bounded In Circle.
71+
```

0 commit comments

Comments
 (0)