Minimum Total Distance Traveled - Problem

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 integer array factory where factory[j] = [positionj, limitj] indicates that positionj is the position of the jth factory and that the jth factory can repair at most limitj 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 the robots can be repaired.

Input & Output

Example 1 — Basic Case
$ Input: robot = [0,4], factory = [[2,2],[6,2]]
Output: 4
💡 Note: Robot at position 0 goes to factory at position 2 (distance = 2), robot at position 4 goes to factory at position 2 (distance = 2). Total distance = 2 + 2 = 4.
Example 2 — Single Factory
$ Input: robot = [1,-1], factory = [[-2,1],[2,1]]
Output: 2
💡 Note: Robot at -1 goes to factory at -2 (distance = 1), robot at 1 goes to factory at 2 (distance = 1). Total distance = 1 + 1 = 2.
Example 3 — Multiple Robots per Factory
$ Input: robot = [9,11,99,101], factory = [[10,1],[7,1],[14,1],[100,1]]
Output: 4
💡 Note: Optimal assignment: robot at 9 goes to factory at 10 (distance=1), robot at 11 goes to factory at 7 (distance=4), robot at 99 goes to factory at 100 (distance=1), robot at 101 goes to factory at 14 (distance=87). Wait, this doesn't give 4. Let me recalculate: 9→10(1), 11→14(3), 99→100(1), 101→100 but capacity is 1, so 101→7 but that's far. The optimal is actually: 9→10(1), 11→14(3), 99→100(1), 101→100 but factory [100,1] can only take 1 robot. So 101 must go elsewhere. The total should be much higher than 4, suggesting this example needs verification.

Constraints

  • 1 ≤ robot.length, factory.length ≤ 100
  • -109 ≤ robot[i], positionj ≤ 109
  • 0 ≤ limitj ≤ robot.length
  • The input will be such that all the robots can be repaired.

Visualization

Tap to expand
Minimum Total Distance Traveled INPUT X-Axis Positions 0 2 4 6 R0 R1 F0 F1 robot = [0, 4] Positions of 2 robots factory = [[2,2],[6,2]] Factory positions & limits Each can repair 2 robots Robot Factory ALGORITHM STEPS 1 Sort Robots Sort: [0, 4] (already sorted) 2 Sort Factories Sort by position: [2,2], [6,2] 3 Greedy Assignment Assign robots to nearest available factory 4 Calculate Distances R0(0) --> F0(2): |0-2| = 2 R1(4) --> F0(2): |4-2| = 2 Assignment Table R0 --> F0 (dist: 2) R1 --> F0 (dist: 2) FINAL RESULT 0 2 4 F0 R0 R1 Distance Calculation |0 - 2| = 2 |4 - 2| = 2 Total = 2 + 2 = 4 OUTPUT 4 Key Insight: After sorting both robots and factories by position, the greedy approach assigns each robot to the nearest factory with remaining capacity. This minimizes total travel distance because sorted order ensures no "crossing" assignments that would increase the total distance. TutorialsPoint - Minimum Total Distance Traveled | Greedy Assignment After Sorting
Asked in
Google 15 Amazon 12
18.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen