[Python] 2 solutions - Picture explain - Clean & Concise - O(N)
\u2714\uFE0F Solution 1: Bruteforce Solution\n\nclass Solution(object):\n    def minOperations(self, boxes):\n        n = len(boxes)\n        ans = [0] * n\n   
**\u2714\uFE0F Solution 1: Bruteforce Solution**\n```\nclass Solution(object):\n    def minOperations(self, boxes):\n        n = len(boxes)\n        ans = [0] * n\n        for i in range(n):\n            for j in range(n):\n                if boxes[j] == \'1\':\n                    ans[i] += abs(j-i)\n        return ans\n```\n**Complexity**\n- Time: `O(N^2)`, where `N <= 2000` is length of `boxes`.\n- Space: `O(1)`\n\n---\n\n**\u2714\uFE0F Solution 2: DP Left to Right and Right To Left**\n- Let `leftDist[i]` is the total distance after moving all the ball from the left side to **ith box**.\n- Let `leftBallCnt` is the number of balls after moving all the ball from the left side to **ith box** so far.\n- To calculate `leftDist[i]`, we need to move `leftBallCnt` balls from **(i-1)th box** by one, so total distance:\n\t- `leftDist[i] = leftDist[i-1] + leftBallCnt`\n- Do the same with `rightDist[i]` and `rightBallCnt` in the right side. \n- To calculate total distance after moving all the ball from the left and the right side to **ith box**:\n\t- `ans[i] = leftDist[i] + rightDist[i]`\n\n![image](https://assets.leetcode.com/users/images/aa1c731c-ca2a-4bed-bfe5-06e3abe2e8d3_1626259914.2228394.png)\n\n\n```python\nclass Solution(object):\n    def minOperations(self, boxes):\n        n = len(boxes)\n\n        leftDist = [0] * n\n        leftBallCnt = int(boxes[0])\n        for i in range(1, n):\n            leftDist[i] = leftDist[i - 1] + leftBallCnt\n            leftBallCnt = leftBallCnt + int(boxes[i])\n\n        rightDist = [0] * n\n        rightBallCnt = int(boxes[n - 1])\n        for i in range(n - 2, -1, -1):\n            rightDist[i] = rightDist[i + 1] + rightBallCnt\n            rightBallCnt = rightBallCnt + int(boxes[i])\n\n        ans = [0] * n\n        for i in range(n):\n            ans[i] = leftDist[i] + rightDist[i]\n        return ans\n```\n**Complexity**\n- Time: `O(N)`, where `N <= 2000` is length of `boxes`.\n- Space: `O(N)`
[]