Skip to content

Commit 3ec32d6

Browse files
committed
Add Robots Collision algorithm and tests
1 parent 42007c8 commit 3ec32d6

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.others;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* The RobotsCollision class provides a method to find the earliest time
7+
* at which any two robots collide on a 1D line.
8+
*
9+
* Each robot has an initial position and a direction ('L' or 'R').
10+
* All robots move simultaneously at the same speed we can assume 1 unit.
11+
* A collision happens when an R-moving robot meets an L-moving robot.
12+
*
13+
*/
14+
public final class RobotsCollision {
15+
private RobotsCollision() {
16+
}
17+
/**
18+
* Finds the earliest time at which any two robots collide.
19+
*
20+
* @param n an integer representing the number of robots
21+
* @param positions an array of integers representing initial positions of robots
22+
* @param directions a string of 'L' and 'R' representing movement directions
23+
* @return the earliest collision time, or -1 if no collision occurs
24+
*/
25+
public static int earliestCollisionTime(int n, int[] positions, String directions) {
26+
27+
// stack keeps track of R-moving robots waiting to collide
28+
Stack<Integer> stack = new Stack<>();
29+
30+
int minTime = Integer.MAX_VALUE;
31+
boolean collisionFound = false;
32+
33+
for (int i = 0; i < n; i++) {
34+
char dir = directions.charAt(i);
35+
36+
if (dir == 'R') {
37+
// moving right, save its position for later
38+
stack.push(positions[i]);
39+
} else {
40+
// moving left — if there's an R robot behind it, they'll collide
41+
if (!stack.isEmpty()) {
42+
int rPosition = stack.pop();
43+
44+
// time = half the gap between them (both moving toward each other)
45+
int time = (positions[i] - rPosition) / 2;
46+
minTime = Math.min(minTime, time);
47+
collisionFound = true;
48+
}
49+
// no R robot behind it, this one just moves left forever
50+
}
51+
}
52+
53+
// if nothing collided at all, return -1
54+
return collisionFound ? minTime : -1;
55+
}
56+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
class RobotsCollisionTest {
9+
10+
// basic example — two pairs colliding, find the earliest
11+
@Test
12+
void testBasicCollision() {
13+
int[] positions = {3, 5, 8, 10};
14+
String directions = "RLRL";
15+
assertEquals(1, RobotsCollision.earliestCollisionTime(4, positions, directions));
16+
}
17+
18+
// no collision at all — all moving in same direction
19+
@Test
20+
void testNoCollision() {
21+
int[] positions = {1, 3, 5, 7};
22+
String directions = "RRRR";
23+
assertEquals(-1, RobotsCollision.earliestCollisionTime(4, positions, directions));
24+
}
25+
26+
// all moving left — no one to crash into
27+
@Test
28+
void testAllMovingLeft() {
29+
int[] positions = {2, 5, 8};
30+
String directions = "LLL";
31+
assertEquals(-1, RobotsCollision.earliestCollisionTime(3, positions, directions));
32+
}
33+
34+
// just two robots heading toward each other
35+
@Test
36+
void testTwoRobotsCollide() {
37+
int[] positions = {1, 5};
38+
String directions = "RL";
39+
// gap is 4, time = 4/2 = 2
40+
assertEquals(2, RobotsCollision.earliestCollisionTime(2, positions, directions));
41+
}
42+
43+
// only one robot — can't collide with anyone
44+
@Test
45+
void testSingleRobot() {
46+
int[] positions = {4};
47+
String directions = "R";
48+
assertEquals(-1, RobotsCollision.earliestCollisionTime(1, positions, directions));
49+
}
50+
51+
// multiple cases in one shot using parameterized test
52+
@ParameterizedTest
53+
@CsvSource({
54+
"2, '1,3', 'RL', 1", // gap of 2 → time 1
55+
"2, '0,10', 'RL', 5", // gap of 10 → time 5
56+
"2, '5,5', 'RL', 0" // same spot → already colliding
57+
})
58+
void testVariousCollisionTimes(int n, String posStr, String directions, int expected) {
59+
// parse the position string into an int array
60+
String[] parts = posStr.split(",");
61+
int[] positions = new int[parts.length];
62+
for (int i = 0; i < parts.length; i++) {
63+
positions[i] = Integer.parseInt(parts[i].trim());
64+
}
65+
assertEquals(expected, RobotsCollision.earliestCollisionTime(n, positions, directions));
66+
}
67+
}

0 commit comments

Comments
 (0)