O(N) Python, incremental approach
You just need to solve it for the first entry and do the rest incrementally. For that end, we keep the number of 1\'s on the right side and left side. The intui
You just need to solve it for the first entry and do the rest incrementally. For that end, we keep the number of 1\'s on the right side and left side. The intuition is that, each time we move to the right, *r* 1\'s get closer to us and *l* 1\'s get further away from us where *r* is the number of 1\'s on the right and *l* is the number of 1\'s on the left.\n\n\'\'\'\n\n    def minOperations(self, boxes: str) -> List[int]:\n        on_left  = 0\n        on_right = 0\n        result   = []\n        \n        # Compute the value for the first entry (O(n)) and update the number of 1s on the right (including the first index)\n        counter  = 0 # Stores the totall number of moves necessary\n        for i, b in enumerate(boxes):\n            if b == \'1\':\n                on_right += 1\n                counter  += i \n                \n        # Move forward and compute other entries incrementally (O(n))\n        for i in range(len(boxes)):\n            if i > 0:\n                counter = counter - on_right + on_left\n            result.append(counter)\n            if boxes[i] == \'1\':\n                on_left  += 1\n                on_right -= 1\n                \n        return result\n\n\n\'\'\'
[]