1769. Minimum Number of Operations to Move All Balls to Each Box

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1769

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