The Minimum Total Distance Traveled problem gives you a list of robots on a number line and a list of factories, each with a position and capacity. Each robot must be assigned to a factory (within its capacity), and the cost is the absolute distance traveled. Minimize the total distance traveled by all robots. This hard Minimum Total Distance Traveled coding problem requires sorting and dynamic programming over the merged sorted sequence.
Microsoft, Infosys, Google, and Bloomberg ask this because it tests the optimal assignment of resources to facilities on a number line — a classic operations research problem. The key insight that sorting both robots and factories and then applying DP eliminates the need for complex matching algorithms. The array, sorting, and dynamic programming interview pattern is comprehensively tested.
Sort both + DP. Sort robots by position. Expand factories into individual slots (a factory with capacity c at position p creates c separate "slots" at position p). Sort these slots. Now the problem reduces to: given sorted robots and sorted factory slots, find the optimal assignment where robot i is assigned to some slot j ≥ i (maintaining order). DP: dp[i][j] = minimum cost assigning first i robots to first j slots. Transition: assign robot i to slot j, or skip slot j. Final: dp[n][m].
Robots: [0, 4, 6]. Factory: (2, 1) capacity 1, (9, 1) capacity 1, (5, 1) capacity 1. Sort robots: [0, 4, 6]. Sort slots: [2, 5, 9].
Assignment problems on a number line are almost always solved by sorting both sides and applying DP with an order-preserving constraint. The "expand factory capacity to individual slots" transformation is the key preprocessing step. After this, the DP structure is identical to sequence alignment or optimal matching DP. Practice 2D DP assignment problems — they form a recurring pattern in logistics optimization interviews at companies like Google and Bloomberg.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Cost to Cut a Stick | Hard | Solve | |
| Jump Game V | Hard | Solve | |
| Maximize Consecutive Elements in an Array After Modification | Hard | Solve | |
| Find the Sum of Subsequence Powers | Hard | Solve | |
| Maximum Height by Stacking Cuboids | Hard | Solve |