-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRobotBoundedInCircle.java
66 lines (59 loc) · 2.48 KB
/
RobotBoundedInCircle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.leetcode.problems.medium;
public class RobotBoundedInCircle {
static Point[] directions = new Point[]{
new Point(Direction.NORTH, 0, 1),
new Point(Direction.EAST, 1, 0),
new Point(Direction.SOUTH, 0, -1),
new Point(Direction.WEST, -1, 0),
};
enum Direction {
NORTH, EAST, SOUTH, WEST
}
public static void main(String[] args) {
System.out.println(isRobotBounded("GGLLGG"));
System.out.println(isRobotBounded("GG"));
System.out.println(isRobotBounded("GL"));
System.out.println(isRobotBounded("GRG"));
}
//
// North
// |
//. West----|-----East
// |
// |
// South
//
public static boolean isRobotBounded(String instructions) {
int currentDirectionIndex = 0;
int currX = 0, currY = 0; // Initially robot is at origin
Direction facingToward = Direction.NORTH;
for (char instruction : instructions.toCharArray()) {
if (instruction == 'G') {
currX += directions[currentDirectionIndex].incrementInX;
currY += directions[currentDirectionIndex].incrementInY;
} else if (instruction == 'L') {
// Rotate left 90Degrees
// So whenever we have to increment in Circular array
// we do (index + 1) % length of Array, to round off back to 0 index in case we overflow
// Similarly whenever we have to decrement
// we do (index + length(arr)) - 1 % length(arr) to do the round off in case we go in negative index.
currentDirectionIndex = ((currentDirectionIndex + directions.length) - 1) % directions.length;
facingToward = directions[currentDirectionIndex].direction;
} else if (instruction == 'R') {
currentDirectionIndex = (currentDirectionIndex + +1) % directions.length;
facingToward = directions[currentDirectionIndex].direction;
}
}
return currX == 0 && currY == 0 || facingToward != Direction.NORTH;
}
static class Point {
Direction direction;
int incrementInX;
int incrementInY;
public Point(final Direction direction, final int incrementInX, final int incrementInY) {
this.direction = direction;
this.incrementInX = incrementInX;
this.incrementInY = incrementInY;
}
}
}