Minimum Number of Operations to Move All Balls to Each Box - Problem

Imagine you have a row of n boxes lined up in front of you, some empty and some containing exactly one ball. Your task is to figure out: "What's the minimum cost to move all balls to each specific box?"

You're given a binary string boxes where:

  • boxes[i] = '1' means the i-th box contains one ball
  • boxes[i] = '0' means the i-th box is empty

In each operation, you can move one ball from any box to an adjacent box (distance of exactly 1). The goal is to calculate, for each box position, how many total operations it would take to move all balls to that specific box.

Example: If boxes = "110", then:
• To move all balls to box 0: move ball from box 1 → box 0 (1 operation)
• To move all balls to box 1: ball is already there, move ball from box 0 → box 1 (1 operation)
• To move all balls to box 2: move both balls rightward (1+2 = 3 operations)

Return an array where answer[i] represents the minimum operations needed to gather all balls at box i.

Input & Output

example_1.py — Basic Case
$ Input: boxes = "110"
Output: [1, 1, 3]
💡 Note: Box 0: move ball from box 1 to box 0 (1 operation). Box 1: ball already at box 1, move ball from box 0 to box 1 (1 operation). Box 2: move ball from box 0 to box 2 (2 operations) + move ball from box 1 to box 2 (1 operation) = 3 operations total.
example_2.py — Sparse Balls
$ Input: boxes = "001011"
Output: [11, 8, 5, 4, 3, 4]
💡 Note: There are balls at positions 2, 4, and 5. For each target position, calculate the sum of distances from all balls to that position. For example, to move all balls to position 0: |0-2| + |0-4| + |0-5| = 2 + 4 + 5 = 11 operations.
example_3.py — Single Ball
$ Input: boxes = "010"
Output: [1, 0, 1]
💡 Note: Only one ball at position 1. To gather at position 0: 1 operation. To gather at position 1: 0 operations (already there). To gather at position 2: 1 operation.

Constraints

  • n == boxes.length
  • 1 ≤ n ≤ 2000
  • boxes[i] is either '0' or '1'
  • Each box contains at most one ball
  • At least one box contains a ball

Visualization

Tap to expand
Minimum Operations to Move All BallsExample: boxes = "110" → Expected output: [1, 1, 3]1Box 01Box 1Box 2Calculating Operations for Each Target:Target Box 0:1 moveTotal: 0 + 1 = 1 operationTarget Box 1:1 moveTotal: 1 + 0 = 1 operationTarget Box 2:2 moves1 moveTotal: 2 + 1 = 3 operationsFinal Answer[1, 1, 3]Operations needed for each target positionTime: O(n) | Space: O(1)
Understanding the Visualization
1
Identify Ball Positions
Mark all positions that currently contain balls
2
Left Pass Calculation
For each position, calculate operations needed from balls to the left
3
Right Pass Calculation
For each position, calculate operations needed from balls to the right
4
Combine Results
Sum left and right contributions to get total operations for each position
Key Takeaway
🎯 Key Insight: Instead of recalculating distances for each position from scratch, we can use prefix sums to track how the total operations change as we move from one target position to the next, achieving optimal O(n) time complexity.
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
28.9K Views
Medium Frequency
~15 min Avg. Time
1.3K 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