1769. Minimum Number of Operations to Move All Balls to Each Box Medium
1/**
2 * [1769] Minimum Number of Operations to Move All Balls to Each Box
3 *
4 * You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the i^th box is empty, and '1' if it contains one ball.
5 * In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes.
6 * Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the i^th box.
7 * Each answer[i] is calculated considering the initial state of the boxes.
8 *
9 * Example 1:
10 *
11 * Input: boxes = "110"
12 * Output: [1,1,3]
13 * Explanation: The answer for each box is as follows:
14 * 1) First box: you will have to move one ball from the second box to the first box in one operation.
15 * 2) Second box: you will have to move one ball from the first box to the second box in one operation.
16 * 3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
17 *
18 * Example 2:
19 *
20 * Input: boxes = "001011"
21 * Output: [11,8,5,4,3,4]
22 *
23 * Constraints:
24 *
25 * n == boxes.length
26 * 1 <= n <= 2000
27 * boxes[i] is either '0' or '1'.
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
33// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn min_operations(boxes: String) -> Vec<i32> {
39 vec![]
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_1769() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.