[Python3] line sweep
Algo\nWhile sweeping from left to right, the only relevant variable is the number of remaining 1s to the right of current position. Here, we subtract 2 from cnt
**Algo**\nWhile sweeping from left to right, the only relevant variable is the number of remaining `1`s to the right of current position. Here, we subtract 2 from `cnt` to account for the effect that while a number move from right to left of the current pointer, it contributes to `ops` by removing the distances from the right ones and adding the distance from the left ones. \n\n**Implementation**\n```\nclass Solution:\n    def minOperations(self, boxes: str) -> List[int]:\n        ans = []\n        ops = cnt = 0 # count of remaining "1"s\n        for i, x in enumerate(boxes):\n            if x == "1": \n                ops += i\n                cnt += 1\n        \n        for i, x in enumerate(boxes): \n            ans.append(ops)\n            if x == "1": cnt -= 2\n            ops -= cnt\n        return ans \n```\n\n**Analysis**\nTime complexity `O(N)`\nSpace complexity `O(N)`
['Python3']