Java
Java copied to clipboard
[Dynamic Programming ] <Collision Detection of N Robots>
What would you like to Propose?
would like to add a new problem titled "Collision Detection of N Robots" implemented in Java. The problem involves simulating robot movements on a 1D line and finding the earliest time at which any two robots collide. This contribution will be added under the DSA / Simulation / Java category.
Issue details
Problem Description:
We are given N robots positioned on a 1D line. Each robot has:
- an initial position (integer), and
- a direction of movement ('L' for left or 'R' for right`).
- All robots start moving simultaneously at the same speed.
- Your task is to determine the earliest time when any two robots collide.
- If no collision occurs, return -1.
Input
N = 4 positions = [3, 5, 8, 10] directions = "RLRL"
output 2
Code Skeleton
import java.util.*;
public class NRobotsCollision { public static int earliestCollisionTime(int n, int[] positions, String directions) { }