Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};
Loading