> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Number of Operations to Move All

> Tested Python solution for LeetCode 1769 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 1769, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1769   # by problem number
lcpy gen -s minimum_number_of_operations_to_move_all_balls_to_each_box   # by problem name
```

## Problem

\<p>You have \<code>n\</code> boxes. You are given a binary string \<code>boxes\</code> of length \<code>n\</code>, where \<code>boxes\[i]\</code> is \<code>'0'\</code> if the \<code>i\<sup>th\</sup>\</code> box is \<strong>empty\</strong>, and \<code>'1'\</code> if it contains \<strong>one\</strong> ball.\</p>

\<p>In one operation, you can move \<strong>one\</strong> ball from a box to an adjacent box. Box \<code>i\</code> is adjacent to box \<code>j\</code> if \<code>abs(i - j) == 1\</code>. Note that after doing so, there may be more than one ball in some boxes.\</p>

\<p>Return an array \<code>answer\</code> of size \<code>n\</code>, where \<code>answer\[i]\</code> is the \<strong>minimum\</strong> number of operations needed to move all the balls to the \<code>i\<sup>th\</sup>\</code> box.\</p>

\<p>Each \<code>answer\[i]\</code> is calculated considering the \<strong>initial\</strong> state of the boxes.\</p>

### Examples

```
Input: boxes = "110"
Output: [1,1,3]
Explanation: The answer for each box is as follows:
1) First box: you will have to move one ball from the second box to the first box in one operation.
2) Second box: you will have to move one ball from the first box to the second box in one operation.
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
```

```
Input: boxes = "001011"
Output: [11,8,5,4,3,4]
```

### Constraints

* n == boxes.length
* 1 \<= n \<= 2000
* boxes\[i] is either '0' or '1'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_operations_to_move_all_balls_to_each_box/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_operations_to_move_all_balls_to_each_box/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) extra (output excluded)
    def min_operations(self, boxes: str) -> list[int]:
        n = len(boxes)
        answer = [0] * n

        balls = 0
        ops = 0
        for i in range(n):
            answer[i] += ops
            if boxes[i] == "1":
                balls += 1
            ops += balls

        balls = 0
        ops = 0
        for i in range(n - 1, -1, -1):
            answer[i] += ops
            if boxes[i] == "1":
                balls += 1
            ops += balls

        return answer
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) extra (output excluded) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.