Skip to content

Commit 75f2207

Browse files
authored
Update 1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js
1 parent 6f616b0 commit 75f2207

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js

+24
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,27 @@ function helper(str, idx) {
4545
}
4646
return res
4747
}
48+
49+
// another
50+
51+
/**
52+
* @param {string} boxes
53+
* @return {number[]}
54+
*/
55+
const minOperations = function(boxes) {
56+
const n = boxes.length
57+
const res = Array(n).fill(0)
58+
let cnt = 0, sum = 0
59+
for(let i = 0; i < n; i++) {
60+
res[i] = sum
61+
cnt += boxes[i] === '1' ? 1 : 0
62+
sum += cnt
63+
}
64+
cnt = 0, sum = 0
65+
for(let i = n - 1; i >= 0; i--) {
66+
res[i] += sum
67+
cnt += boxes[i] === '1' ? 1 : 0
68+
sum += cnt
69+
}
70+
return res
71+
};

0 commit comments

Comments
 (0)