Imagine you're managing a fleet of broken robots scattered across a long assembly line (represented as the X-axis). These robots need to be repaired at specific repair factories positioned at various points along the line.
Here's the challenge: Each robot can move in either direction along the X-axis, but once you set their initial direction, they move at constant speed until they reach a factory that can repair them. Each factory has a limited capacity - it can only repair a certain number of robots before it's full.
Your goal is to strategically set the initial movement direction for each robot to minimize the total distance traveled by all robots combined.
Key Details:
- Robots never collide - they either move in parallel or pass through each other
- A robot will stop at the first available factory it reaches (that hasn't reached capacity)
- You have full control over each robot's initial direction
- All robots are guaranteed to eventually find a factory
Input: An array robot[] with robot positions, and a 2D array factory[] where each element is [position, capacity]
Output: The minimum total distance that needs to be traveled by all robots
Input & Output
Constraints
- 1 ≤ robot.length, factory.length ≤ 100
- -109 ≤ robot[i], factory[j][0] ≤ 109
- 0 ≤ factory[j][1] ≤ robot.length
- All robot positions are unique
- All factory positions are unique
- Sum of all factory[j][1] ≥ robot.length (all robots can be repaired)