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.length
1 <= n <= 2000
boxes[i]
is either'0'
or'1'
.
Approach 01:
-
C++
-
Python
#include <vector> #include <string> using namespace std; class Solution { public: vector<int> minOperations(string boxes) { int n = boxes.length(); vector<int> operationsResult(n, 0); // Stores the final operations count for each box int ballCount = 0; // Tracks the number of balls ('1's) encountered so far int currentOperations = 0; // Tracks the cumulative operations while traversing // Traverse from left to right for (int i = 0; i < n; ++i) { operationsResult[i] += currentOperations; ballCount += (boxes[i] - '0'); // Increment ball count if the box has a ball currentOperations += ballCount; // Update the number of operations } ballCount = 0; currentOperations = 0; // Traverse from right to left for (int i = n - 1; i >= 0; --i) { operationsResult[i] += currentOperations; ballCount += (boxes[i] - '0'); // Increment ball count if the box has a ball currentOperations += ballCount; // Update the number of operations } return operationsResult; } };
class Solution: def minOperations(self, boxes: str) -> List[int]: n = len(boxes) result = [0] * n count = 0 operations = 0 # Traverse from left to right for i in range(n): result[i] += operations count += int(boxes[i]) # Count the number of '1's encountered so far operations += count # Update the number of operations count = 0 operations = 0 # Traverse from right to left for i in range(n - 1, -1, -1): result[i] += operations count += int(boxes[i]) # Count the number of '1's encountered so far operations += count # Update the number of operations return result
Time Complexity:
- Left-to-Right Traversal:
The algorithm iterates over the string once, resulting in \( O(n) \), where \( n \) is the length of the string.
- Right-to-Left Traversal:
Another iteration over the string contributes an additional \( O(n) \).
- Overall Time Complexity:
\( O(n) \), as both traversals are sequential and independent.
Space Complexity:
- Result Array:
The array `operationsResult` requires \( O(n) \) space to store the results.
- Auxiliary Variables:
Constant space is used for variables like `ballCount` and `currentOperations`, contributing \( O(1) \).
- Overall Space Complexity:
\( O(n) \), as the result array dominates the space usage.