You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith box is empty, and '1' if it contains one ball.
In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes.
Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box.
Each answer[i] is calculated considering the initial state of the boxes.
Example 1:
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.
Example 2:
Input: boxes = "001011" Output: [11,8,5,4,3,4]
Constraints:
n == boxes.length1 <= n <= 2000boxes[i] is either '0' or '1'.The simplest way to solve this problem is by iterating through each box, and for each box, calculate the number of moves required to bring all balls to that box by checking every other box. For each pair of boxes (i, j), a ball present at box j needs |j - i| moves to reach box i.
This straight-forward approach has a time complexity of O(n^2) since for every position, we are traversing the list of boxes.
This solution iterates through each possible target box and, for each box, counts the required number of moves by checking each ball's current position. The operations counted for a ball at position j moving to position i is the absolute difference |j - i|.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) because it requires traversing the entire boxes string for each target box.
Space Complexity: O(n) for storing the result.
This approach significantly reduces redundant calculations by using prefix sums. First, make one pass from left to right to calculate the number of operations needed to bring balls to each position considering only balls to its left. Next, make another pass from right to left to factor in the balls on the right. By separating these concerns, we use prefix sums to compute the required operations in linear time.
In essence, it accumulates the number of moves by maintaining a running total of moves per box as you encounter them through the linear scan.
This C program makes two passes over the input string. In the first pass, it calculates the cost of moving balls from the left of each box, and in the second pass, it does the same for balls on the right.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) because it makes two linear passes over the input.
Space Complexity: O(n) for storing the result array.
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2) because it requires traversing the entire boxes string for each target box. |
| Optimized Linear Pass | Time Complexity: O(n) because it makes two linear passes over the input. |
Minimum Number of Operations to Move All Balls to Each Box - Leetcode 1769 - Python • NeetCodeIO • 11,998 views views
Watch 9 more video solutions →Practice Minimum Number of Operations to Move All Balls to Each Box with our built-in code editor and test cases.
Practice on FleetCode