Skip to content

Commit e7f49ab

Browse files
committed
Add solution #1041
1 parent 1d508a5 commit e7f49ab

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
1009|[Complement of Base 10 Integer](./1009-complement-of-base-10-integer.js)|Easy|
166166
1010|[Pairs of Songs With Total Durations Divisible by 60](./1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
167167
1037|[Valid Boomerang](./1037-valid-boomerang.js)|Easy|
168+
1041|[Robot Bounded In Circle](./1041-robot-bounded-in-circle.js)|Medium|
168169
1103|[Distribute Candies to People](./1103-distribute-candies-to-people.js)|Easy|
169170
1108|[Defanging an IP Address](./1108-defanging-an-ip-address.js)|Easy|
170171
1189|[Maximum Number of Balloons](./1189-maximum-number-of-balloons.js)|Easy|
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1041. Robot Bounded In Circle
3+
* https://leetcode.com/problems/robot-bounded-in-circle/
4+
* Difficulty: Medium
5+
*
6+
* On an infinite plane, a robot initially stands at (0, 0) and faces north.
7+
* The robot can receive one of three instructions:
8+
* - "G": go straight 1 unit;
9+
* - "L": turn 90 degrees to the left;
10+
* - "R": turn 90 degrees to the right.
11+
*
12+
* The robot performs the instructions given in order, and repeats them forever.
13+
* Return true if and only if there exists a circle in the plane such that the
14+
* robot never leaves the circle.
15+
*/
16+
17+
/**
18+
* @param {string} instructions
19+
* @return {boolean}
20+
*/
21+
var isRobotBounded = function(instructions) {
22+
let x = 0, y = 0;
23+
let dx = 0, dy = 1;
24+
25+
for (const direction of instructions) {
26+
switch (direction) {
27+
case 'R': [dx, dy] = [dy, -dx]; break;
28+
case 'L': [dy, dx] = [dx, -dy]; break;
29+
case 'G': [x, y] = [x + dx, y + dy]; break;
30+
}
31+
}
32+
33+
return (x === 0 && y === 0) || dy !== 1;
34+
}

0 commit comments

Comments
 (0)