81. Robot Bounded in Circle
18 Sep 2020 | Daily Algorithms
- I expect this problem to be medium problem.
Intuition
- Let chopper help explain.
- Starting at the origin and face north (0,1), after one sequence of instructions
- if chopper return to the origin, he is obvious in an circle.
- if chopper finishes with face not towards north, it will get back to the initial status in another one or three sequences.
Explanation
- (x,y) is the location of chopper.
- d[i] is the direction he is facing.
- i = (i + 1) % 4 will turn right
- i = (i + 3) % 4 will turn left
- Check the final status after instructions.
Complexity
Time O(N)
Space O(1)
class Solution {
public:
bool isRobotBounded(string instructions) {
int x = 0, y = 0, i = 0;
vector<vector<int>> d = {0, 1}, {1, 0}, {0, -1}, { -1, 0}; // 원래는 {} 하나 더
// facing : north, rigth, south, left
for(char & ins : instructions)
{
if(ins =='R')
{
i = (i+1) % 4;
}
else if (ins =='L')
{
i = (i+3) % 4;
}
else
{
x += d[i][0], y += d[i][1];
}
}
return x==0 && y==0 || i>0;
}
};
- I expect this problem to be medium problem.
Intuition
- Let chopper help explain.
- Starting at the origin and face north (0,1), after one sequence of instructions
- if chopper return to the origin, he is obvious in an circle.
- if chopper finishes with face not towards north, it will get back to the initial status in another one or three sequences.
Explanation
- (x,y) is the location of chopper.
- d[i] is the direction he is facing.
- i = (i + 1) % 4 will turn right
- i = (i + 3) % 4 will turn left
- Check the final status after instructions.
Complexity
Time O(N) Space O(1)
class Solution {
public:
bool isRobotBounded(string instructions) {
int x = 0, y = 0, i = 0;
vector<vector<int>> d = {0, 1}, {1, 0}, {0, -1}, { -1, 0}; // 원래는 {} 하나 더
// facing : north, rigth, south, left
for(char & ins : instructions)
{
if(ins =='R')
{
i = (i+1) % 4;
}
else if (ins =='L')
{
i = (i+3) % 4;
}
else
{
x += d[i][0], y += d[i][1];
}
}
return x==0 && y==0 || i>0;
}
};
Comments