|
| 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 | + */ |
| 30 | +pub 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 | + |
| 37 | +impl Solution { |
| 38 | + // Credit: https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/solutions/5743499/improved-algorithm-with-o-n-runtime/ |
| 39 | + pub fn min_operations(boxes: String) -> Vec<i32> { |
| 40 | + let n = boxes.len(); |
| 41 | + let mut result: Vec<i32> = vec![0; n]; |
| 42 | + let mut moves: i32 = 0; |
| 43 | + let mut count: i32 = 0; |
| 44 | + |
| 45 | + for i in 0..n { |
| 46 | + result[i] += moves; |
| 47 | + if boxes.chars().nth(i) == Some('1') { |
| 48 | + count += 1; |
| 49 | + } |
| 50 | + moves += count; |
| 51 | + } |
| 52 | + |
| 53 | + moves = 0; |
| 54 | + count = 0; |
| 55 | + |
| 56 | + for i in (0..n).rev() { |
| 57 | + result[i] += moves; |
| 58 | + if boxes.chars().nth(i) == Some('1') { |
| 59 | + count += 1; |
| 60 | + } |
| 61 | + moves += count; |
| 62 | + } |
| 63 | + result |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// submission codes end |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use super::*; |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_1769_example_1() { |
| 75 | + let boxes = "110".to_string(); |
| 76 | + |
| 77 | + let result = vec![1, 1, 3]; |
| 78 | + |
| 79 | + assert_eq!(Solution::min_operations(boxes), result); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_1769_example_2() { |
| 84 | + let boxes = "001011".to_string(); |
| 85 | + |
| 86 | + let result = vec![11, 8, 5, 4, 3, 4]; |
| 87 | + |
| 88 | + assert_eq!(Solution::min_operations(boxes), result); |
| 89 | + } |
| 90 | +} |
0 commit comments