explained solution
let\'s consider this example - "001001". For this we don\'t want to calculate for each 1\'s , how many movement it needs to move, instead we will use previous c
let\'s consider this example - "001001". For this we don\'t want to calculate for each 1\'s , how many movement it needs to move, instead we will use previous calculated result and see how to optimize it. \n<img src="https://assets.leetcode.com/users/images/6145b6dd-d653-4c66-af54-83d5c6048b74_1658907426.8135538.png" width ="600" >\n\nfirstly i will calculate movement for all balls to reach 0th index that is sum of all indexes, along with this we will keep a count of no. of 1\'s (let say rightones) .\n\n<img src="https://assets.leetcode.com/users/images/8ae0122c-f7d9-4047-bb9b-7abd1ea83495_1658910100.8864417.png" width = "600">\n\nsee it took all the 3 balls on the right to reach 11 operations for 0th index. for index = 1, they will take 11 operations to reach 0 and -3 operations for all balls (as we are traversing back, consider as taking steps back) to go back 1 step i.e 11 - 3 = 8. \n\n<img src="https://assets.leetcode.com/users/images/9dd64e32-85d2-42f9-8448-f209e5580921_1658910160.1058989.png" width = "600" >\n\nat index = 2, they will take 8 stpes to reach index 1 and then all three of them will take 1 step back  i.e 8 - 3 = 5\n\n**So basically if i know there is total B balls on the right (including current index ) and i know it took V operations for the previous index then for the current index, it will take *V- B* operations .**\n\nnow at index = 3, i can see there are 2 balls that will take 5 operations for index 2 and there is 1 ball on the left which will need to traverse 1 step ahaed then total steps = 5 - 2 + 1 => 4\n\n<img src="https://assets.leetcode.com/users/images/f2d188ba-9c55-4718-a5d3-5c1b934d4453_1658910941.8645532.png" width = "600">\n\n**If there are K balls on the left then i need to add them as they took V operations to reach (i-1) index and +1 operations for each K ball to reach at index i** \n\n> **Operations for ith index = operations for (i-1) index - total balls on the right including ith index + total balls on the left  .**\n\nHope this explains how to proceed for O(n) solutions with 2 pass (preprocessing).\n\n```\nclass Solution {\n    public int[] minOperations(String boxes) {\n        int n = boxes.length();\n     int ans[] = new int[n];\n     int count = 0;\n     int index = 0;\n        for(int i = 0 ; i < n ; ++i ){\n            if(boxes.charAt(i) == \'1\'){\n                ++count;\n                index += i;\n            }\n        }\n        ans[0] = index;\n        \n        int fcount = 0;  // 1\'s that i processed \n        if(boxes.charAt(0) == \'1\'){\n            count--;\n            fcount++;\n        }\n        for(int i = 1 ; i < n ; ++i){\n            ans[i] = ans[i-1] - count + fcount;\n            if(boxes.charAt(i) == \'1\'){\n            count--;\n            fcount++;\n        }\n        }\n        return ans;\n    }\n}\n```\n\n\n\n
[]