Skip to content

Commit 58e95ca

Browse files
authored
Create 1769. Minimum Number of Operations to Move All Balls to Each Box (#683)
2 parents d5133ea + ccb2180 commit 58e95ca

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<int> minOperations(string boxes) {
4+
int n = boxes.size();
5+
vector<int> answer(n, 0);
6+
7+
int ballsToLeft = 0, movesToLeft = 0;
8+
int ballsToRight = 0, movesToRight = 0;
9+
10+
// Single pass: calculate moves from both left and right
11+
for (int i = 0; i < n; i++) {
12+
// Left pass
13+
answer[i] += movesToLeft;
14+
ballsToLeft += boxes[i] - '0';
15+
movesToLeft += ballsToLeft;
16+
17+
// Right pass
18+
int j = n - 1 - i;
19+
answer[j] += movesToRight;
20+
ballsToRight += boxes[j] - '0';
21+
movesToRight += ballsToRight;
22+
}
23+
24+
return answer;
25+
}
26+
};

0 commit comments

Comments
 (0)