1256. Minimum Total Distance Traveled

HardDynamic ProgrammingPartition DP

Robots are at given positions on a line, and factories are given as [position, limit] where limit is the maximum robots that factory can repair. Every robot must move (left or right) to some factory, costing the distance travelled. Return the minimum total distance travelled by all robots. The input is JSON {robot, factory}.

Input: JSON {robot, factory}.

Output: Integer — the minimum total distance.

Examples

Example 1
Input: {"robot":[0,4,6],"factory":[[2,2],[6,2]]}
Output: 4
Explanation: Two robots go to factory 2, one is already at 6.
Example 2
Input: {"robot":[1,-1],"factory":[[-2,1],[2,1]]}
Output: 2
Explanation: Each robot moves one unit.
Example 3
Input: {"robot":[0],"factory":[[0,1]]}
Output: 0
Explanation: Already in place.

Constraints

Asked by

InfosysBloombergGoogleMicrosoftAmazon
Solve this problem in the editor →