Tap here to switch tabs
Problem
Submissions

Problem: Minimum Total Distance Traveled

Try to solve the Minimum Total Distance Traveled problem.

Statement

There are several robots and factories located on the XX axis. You are given an integer array robot, where robot[i] represents the position of the iextthi^{ ext{th}} robot. You are also given a 22D integer array factory, where factory[j] = [positionj_j, limitj_j] indicates that the jextthj^{ ext{th}} factory is located at $ ext{position}_j$ and can repair at most $ ext{limit}_j$ robots.

Each robot must be assigned to a factory for repair such that no factory repairs more robots than its capacity limit. The distance a robot travels is the absolute difference between its initial position and the position of its assigned factory.

Return the minimum total distance traveled by all robots to repair every robot.

Note: The input is generated such that it is always possible to repair every robot.

Constraints:

  • 1≤1 \leq robot.length, factory.length ≤100\leq 100

  • factory[j].length =2= 2

  • −109≤-10^9 \leq robot[i], $ ext{position}_j \leq 10^9$

  • 0≤extlimitj≤0 \leq ext{limit}_j \leq robot.length

  • The input will be generated such that it is always possible to repair every robot.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Total Distance Traveled

Try to solve the Minimum Total Distance Traveled problem.

Statement

There are several robots and factories located on the XX axis. You are given an integer array robot, where robot[i] represents the position of the iextthi^{ ext{th}} robot. You are also given a 22D integer array factory, where factory[j] = [positionj_j, limitj_j] indicates that the jextthj^{ ext{th}} factory is located at $ ext{position}_j$ and can repair at most $ ext{limit}_j$ robots.

Each robot must be assigned to a factory for repair such that no factory repairs more robots than its capacity limit. The distance a robot travels is the absolute difference between its initial position and the position of its assigned factory.

Return the minimum total distance traveled by all robots to repair every robot.

Note: The input is generated such that it is always possible to repair every robot.

Constraints:

  • 1≤1 \leq robot.length, factory.length ≤100\leq 100

  • factory[j].length =2= 2

  • −109≤-10^9 \leq robot[i], $ ext{position}_j \leq 10^9$

  • 0≤extlimitj≤0 \leq ext{limit}_j \leq robot.length

  • The input will be generated such that it is always possible to repair every robot.