517. Super Washing Machines Hard

@problem@discussion
#Array#Greedy



1/**
2 * [517] Super Washing Machines
3 *
4 * You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
5 * For each move, you could choose any m (1 <= m <= n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
6 * Given an integer array machines representing the number of dresses in each washing machine from left to right on the line, return the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
7 *  
8 * Example 1:
9 * 
10 * Input: machines = [1,0,5]
11 * Output: 3
12 * Explanation:
13 * 1st move:    1     0 <-- 5    =>    1     1     4
14 * 2nd move:    1 <-- 1 <-- 4    =>    2     1     3
15 * 3rd move:    2     1 <-- 3    =>    2     2     2
16 * 
17 * Example 2:
18 * 
19 * Input: machines = [0,3,0]
20 * Output: 2
21 * Explanation:
22 * 1st move:    0 <-- 3     0    =>    1     2     0
23 * 2nd move:    1     2 --> 0    =>    1     1     1
24 * 
25 * Example 3:
26 * 
27 * Input: machines = [0,2,0]
28 * Output: -1
29 * Explanation:
30 * It's impossible to make all three washing machines have the same number of dresses.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	n == machines.length
36 * 	1 <= n <= 10^4
37 * 	0 <= machines[i] <= 10^5
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/super-washing-machines/
43// discuss: https://leetcode.com/problems/super-washing-machines/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn find_min_moves(machines: Vec<i32>) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_517() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.