1769. Minimum Number of Operations to Move All Balls to Each Box
On This Page
class Solution:
def minOperations(self, boxes: str) -> List[int]:
n = len(boxes)
res = [0] * n
cnt = 0
for i in range(1, n):
if boxes[i - 1] == '1':
cnt += 1
res[i] = res[i - 1] + cnt
cnt = s = 0
for i in range(n - 2, -1, -1):
if boxes[i + 1] == '1':
cnt += 1
s += cnt
res[i] += s
return res