369. Minimum Total Distance Traveled
There are some robots and factories on the X-axis. You are given an integer array robot where robot[i] is the position of the ith robot. You are also given a 2D array factory where factory[j] = [position_j, limit_j] indicates that position_j is the position of the jth factory and that the jth factory can repair at most limit_j robots. The positions of each robot are unique. The positions of each factory are also unique. Note that a robot can be in the same position as a factory initially. All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving. At any moment, you can set the initial direction of moving for some robot. Your target is to minimize the total distance traveled by all the robots. Return the minimum total distance traveled by all the robots. The test cases are generated such that all robots can be repaired.
Examples
Input: [0,4,6] [[2,2],[6,2]]
Output: 4
Explanation: Public test case for Minimum Total Distance Traveled
Constraints
- 1 <= robot.length, factory.length <= 100
- factory[j].length == 2
- -10^9 <= robot[i], factory[j][0] <= 10^9
- 0 <= factory[j][1] <= robot.length
- The input is generated such that it is always possible to repair every robot.
Run checks all cases above. Submit evaluates all test cases.