Skip to content

Commit

Permalink
Create 1769. Minimum Number of Operations to Move All Balls to Each B…
Browse files Browse the repository at this point in the history
…ox (#683)
  • Loading branch information
Chayandas07 authored Jan 6, 2025
2 parents d5133ea + ccb2180 commit 58e95ca
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 1769. Minimum Number of Operations to Move All Balls to Each Box
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
vector<int> minOperations(string boxes) {
int n = boxes.size();
vector<int> 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;
}
};

0 comments on commit 58e95ca

Please sign in to comment.