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 ballboxes[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
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