Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Move All Balls to Each Box

Try to solve the Minimum Operations to Move All Balls to Each Box problem.

Statement

You are given a binary string boxes of length n, where boxes[i] is '0' if the box at index i is empty, and '1' if it contains a ball.

In one operation, you can move 11 ball from a box at index j to an adjacent box at index j + 1 or j - 1.

Return an array answer of length n, where answer[i] is the minimum number of operations required to move all the balls to the box at index i.

Constraints:

  • n ==== boxes.length

  • 1≤1 \leq n ≤2000\leq 2000

  • boxes[i] is either '0' or '1'

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Move All Balls to Each Box

Try to solve the Minimum Operations to Move All Balls to Each Box problem.

Statement

You are given a binary string boxes of length n, where boxes[i] is '0' if the box at index i is empty, and '1' if it contains a ball.

In one operation, you can move 11 ball from a box at index j to an adjacent box at index j + 1 or j - 1.

Return an array answer of length n, where answer[i] is the minimum number of operations required to move all the balls to the box at index i.

Constraints:

  • n ==== boxes.length

  • 1≤1 \leq n ≤2000\leq 2000

  • boxes[i] is either '0' or '1'