Minimum Total Distance Traveled - Problem

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

example_1.py — Basic Case
$ Input: robot = [0,4,6], 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). Robot at position 6 goes to factory at position 6 (distance=0). Total distance = 2+2+0 = 4.
example_2.py — Multiple Factories
$ Input: robot = [1,-1], factory = [[-2,1],[2,1]]
Output: 2
💡 Note: Robot at position 1 goes to factory at position 2 (distance=1). Robot at position -1 goes to factory at position -2 (distance=1). Total distance = 1+1 = 2.
example_3.py — Capacity Constraint
$ Input: robot = [9,11,99,101], factory = [[10,1],[7,1],[14,1],[100,1],[96,1],[103,1]]
Output: 6
💡 Note: Optimal assignment: robot 9→factory 10 (dist=1), robot 11→factory 7 (dist=4), robot 99→factory 96 (dist=3), robot 101→factory 103 (dist=2). But this violates the constraint that robot 11 can't reach factory 7 if robot 9 goes to factory 10. The actual optimal is robot positions matched to nearest available factories respecting the movement constraint.

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)

Visualization

Tap to expand
Emergency Response CoordinationP1P2P3Hospital A(3 beds)Hospital B(2 beds)70 units320 units60 unitsDynamic Programming Decision Process:1. Sort patients and hospitals by location2. For each patient-hospital pair, choose: assign now or save hospital bed3. Build solution by combining optimal subproblemsTotal Distance: 450 unitsOptimal assignment minimizes overall travel
Understanding the Visualization
1
Assess Situation
Map out patient locations (robots) and hospital locations with bed counts (factories)
2
Sort by Location
Arrange patients and hospitals by position to minimize crossing paths
3
Dynamic Assignment
Use DP to optimally decide which patients go to which hospitals
4
Execute Plan
Dispatch ambulances following the optimal assignment plan
Key Takeaway
🎯 Key Insight: Sort both arrays first, then use DP to make locally optimal decisions that build up to the globally optimal solution. Never assign robots in a way that causes them to cross paths unnecessarily.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
28.9K Views
Medium Frequency
~35 min Avg. Time
847 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