Skip to content

Commit fee17cb

Browse files
authored
Create robot-bounded-in-circle.cpp
1 parent d0f61db commit fee17cb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/robot-bounded-in-circle.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool isRobotBounded(string instructions) {
7+
static const vector<vector<int>>& directions = {{ 1, 0}, {0, -1},
8+
{-1, 0}, {0, 1}};
9+
int x = 0, y = 0, i = 0;
10+
11+
for (const auto& instruction : instructions) {
12+
if (instruction == 'R') {
13+
i = (i + 1) % 4;
14+
} else if (instruction == 'L') {
15+
i = (4 + i - 1) % 4;
16+
} else {
17+
x += directions[i][0], y += directions[i][1];
18+
}
19+
}
20+
return (x == 0 && y == 0) || i > 0;
21+
}
22+
};

0 commit comments

Comments
 (0)