Skip to content

Commit b966031

Browse files
authored
Create 1769.Minimum-Number-of-Operations-to-Move-All-Balls-to-Each-Box.cpp
1 parent 1a885cd commit b966031

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<int> minOperations(string boxes)
4+
{
5+
int n = boxes.size();
6+
vector<int>leftMoves(n);
7+
vector<int>rightMoves(n);
8+
9+
int left = 0;
10+
for (int i=0; i<n; i++)
11+
{
12+
leftMoves[i] += (i==0 ? 0: leftMoves[i-1]) + left;
13+
left += (boxes[i]=='1');
14+
}
15+
int right = 0;
16+
for (int i=n-1; i>=0; i--)
17+
{
18+
rightMoves[i] += (i==n-1 ? 0: rightMoves[i+1]) + right;
19+
right += (boxes[i]=='1');
20+
}
21+
22+
vector<int>rets(n);
23+
for (int i=0; i<n; i++)
24+
rets[i] = leftMoves[i] + rightMoves[i];
25+
return rets;
26+
}
27+
};

0 commit comments

Comments
 (0)