Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.thealgorithms.greedyalgorithms;

import java.util.Stack;

/**
* The RobotsCollision class provides a method to find the earliest time
* at which any two robots collide on a 1D line.
*
* Each robot has an initial position and a direction ('L' or 'R').
* All robots move simultaneously at the same speed we can assume 1 unit.
* A collision happens when an R-moving robot meets an L-moving robot.
*
*/
public final class RobotsCollision {
private RobotsCollision() {
}
/**
* Finds the earliest time at which any two robots collide.
*
* @param n an integer representing the number of robots
* @param positions an array of integers representing initial positions of robots
* @param directions a string of 'L' and 'R' representing movement directions
* @return the earliest collision time, or -1 if no collision occurs
*/

/*
* R robot = moving right (increasing position)
* L robot = moving left (decreasing position)
*/
public static int earliestCollisionTime(int n, int[] positions, String directions) {
// stack for keeping the track of R moving robots waiting to collide
Stack<Integer> stack = new Stack<>();

int minTime = Integer.MAX_VALUE;
boolean collisionFound = false;

for(int i = 0; i < n; i++){
char dir = directions.charAt(i);
if (dir == 'R') {
// moving right, save its position for later
stack.push(positions[i]);
} else {
// moving left — if there's an R robot behind it, they'll collide
if (!stack.isEmpty()) {
int rPosition = stack.pop();

// time = half the gap between them (both moving toward each other)
int time = (positions[i] - rPosition) / 2;
minTime = Math.min(minTime, time);
collisionFound = true;
}
// no R robot behind it, this one just moves left forever
}
}

// if nothing collided at all, return -1
return collisionFound ? minTime : -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.thealgorithms.greedyalgorithms;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class RobotsCollisionTest {

// basic example — two pairs colliding, find the earliest
@Test
void testBasicCollision() {
int[] positions = {3, 5, 8, 10};
String directions = "RLRL";
assertEquals(1, RobotsCollision.earliestCollisionTime(4, positions, directions));
}

// no collision at all — all moving in same direction
@Test
void testNoCollision() {
int[] positions = {1, 3, 5, 7};
String directions = "RRRR";
assertEquals(-1, RobotsCollision.earliestCollisionTime(4, positions, directions));
}

// all moving left — no one to crash into
@Test
void testAllMovingLeft() {
int[] positions = {2, 5, 8};
String directions = "LLL";
assertEquals(-1, RobotsCollision.earliestCollisionTime(3, positions, directions));
}

// just two robots heading toward each other
@Test
void testTwoRobotsCollide() {
int[] positions = {1, 5};
String directions = "RL";
// gap is 4, time = 4/2 = 2
assertEquals(2, RobotsCollision.earliestCollisionTime(2, positions, directions));
}

// only one robot — can't collide with anyone
@Test
void testSingleRobot() {
int[] positions = {4};
String directions = "R";
assertEquals(-1, RobotsCollision.earliestCollisionTime(1, positions, directions));
}

// multiple cases in one shot using parameterized test
@ParameterizedTest
@CsvSource({
"2, '1,3', 'RL', 1", // gap of 2 - time 1
"2, '0,10', 'RL', 5", // gap of 10 - time 5
"2, '5,5', 'RL', 0" // same spot - already colliding
})
void testVariousCollisionTimes(int n, String posStr, String directions, int expected) {
// parse the position string into an int array
String[] parts = posStr.split(",");
int[] positions = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
positions[i] = Integer.parseInt(parts[i].trim());
}
assertEquals(expected, RobotsCollision.earliestCollisionTime(n, positions, directions));
}
}
Loading