Skip to content

Commit 9d81844

Browse files
authored
Create minimum-number-of-operations-to-move-all-balls-to-each-box.cpp
1 parent 6759b15 commit 9d81844

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> minOperations(string boxes) {
7+
vector<int> result(size(boxes));
8+
for (int i = 0, accu = 0, cnt = 0; i < size(boxes); ++i) {
9+
result[i] += accu;
10+
cnt += (boxes[i] == '1') ? 1 : 0;
11+
accu += cnt;
12+
}
13+
for (int i = size(boxes) - 1, accu = 0, cnt = 0; i >= 0; --i) {
14+
result[i] += accu;
15+
cnt += (boxes[i] == '1') ? 1 : 0;
16+
accu += cnt;
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)