diff --git a/1769. Minimum Number of Operations to Move All Balls to Each Box b/1769. Minimum Number of Operations to Move All Balls to Each Box new file mode 100644 index 0000000..8fcb3f3 --- /dev/null +++ b/1769. Minimum Number of Operations to Move All Balls to Each Box @@ -0,0 +1,26 @@ +class Solution { +public: + vector minOperations(string boxes) { + int n = boxes.size(); + vector answer(n, 0); + + int ballsToLeft = 0, movesToLeft = 0; + int ballsToRight = 0, movesToRight = 0; + + // Single pass: calculate moves from both left and right + for (int i = 0; i < n; i++) { + // Left pass + answer[i] += movesToLeft; + ballsToLeft += boxes[i] - '0'; + movesToLeft += ballsToLeft; + + // Right pass + int j = n - 1 - i; + answer[j] += movesToRight; + ballsToRight += boxes[j] - '0'; + movesToRight += ballsToRight; + } + + return answer; + } +};