Java || DP || beats 100% || 2ms || O(boxes.length) O(1)
\n    public int[] minOperations(String boxes) {\n        int left = 0, right = 0, lsum = 0, rsum = 0, len = boxes.length();\n\t\tfor (int i = 0; i < len; i++) 
\n    public int[] minOperations(String boxes) {\n        int left = 0, right = 0, lsum = 0, rsum = 0, len = boxes.length();\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tif (boxes.charAt(i) == \'1\') {\n\t\t\t\tright++;\n\t\t\t\trsum += i;\n\t\t\t}\n\t\t}\n\n\t\tint[] ans = new int[len];\n\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tans[i] = lsum + rsum;\n\t\t\tif (boxes.charAt(i) == \'1\') {\n\t\t\t\tleft++;\n\t\t\t\tright--;\n\t\t\t}\n\t\t\tlsum = lsum + left;\n\t\t\trsum = rsum - right;\n\t\t}\n\n\t\treturn ans;\n    }\n
[]