From ccb218097184a68aff7416e86409df8dfaaea2a5 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:30:42 +0530 Subject: [PATCH] Create 1769. Minimum Number of Operations to Move All Balls to Each Box --- ...f Operations to Move All Balls to Each Box | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1769. Minimum Number of Operations to Move All Balls to Each Box 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; + } +};