[Leetcode] 517. Super Washing Machines 解题报告

题目

You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.

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 .

Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find 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.

Example1

Input: [1,0,5]

Output: 3

Explanation: 
1st move:    1     0 <-- 5    =>    1     1     4
2nd move:    1 <-- 1 <-- 4    =>    2     1     3    
3rd move:    2     1 <-- 3    =>    2     2     2   

Example2

Input: [0,3,0]

Output: 2

Explanation: 
1st move:    0 <-- 3     0    =>    1     2     0    
2nd move:    1     2 --> 0    =>    1     1     1     

Example3

Input: [0,2,0]

Output: -1

Explanation: 
It's impossible to make all the three washing machines have the same number of dresses. 

Note:

  1. The range of n is [1, 10000].
  2. The range of dresses number in a super washing machine is [0, 1e5].

思路

我们首先计算所有洗衣机里面的衣服总和,如果衣服总和不能被洗衣机个数整除,那么就不存在解决方案。否则,我们总是可以找到移动方案使得所有洗衣机最终的衣服个数相同,最差方案无非是每次只选择一个洗衣机,只移动一件衣服。在这种情况下,移动次数就是所有洗衣机的移动次数之和。

然而更好的消息是:我们每次可以同时移动几个洗衣机里面的衣服。因此最小移动次数就变成了每个洗衣机的必要移动次数的最大值。对于单个洗衣机而言,必要移动次数就是将衣服从它的一侧移动到另外一侧(这里的一侧还同时包含它自身),直到它两边的洗衣机加上它本身的衣服件数都达到了平均值。对于每个洗衣机,其必要移动次数取决于左侧必要移动次数L和右侧必要移动次数R,具体定义就是(所需衣服) - (已有衣服)。根据L和R的正负,具体可以分为三种情况:

1)L > 0 && R > 0:说明两侧都缺衣服,所以我们必须从当前洗衣机一件一件地向两侧匀衣服,所以必要移动次数是:abs(L) + abs(R);

2)L < 0 && R < 0:说明两侧都有多余衣服,但是好消息是我们可以从两边同时向这台洗衣机匀衣服,所以必要移动次数是:max(abs(L), abs(R));

3)L < 0 && R > 0或者L > 0 && R < 0:有多余衣服的一侧需要向缺衣服的一侧匀衣服,所以必要移动次数仍然是:max(abs(L), abs(R))。

例如对于题目中给出的例子[1, 0, 5],平均每个洗衣机里面应该拥有2件衣服,那么:

对于第1台洗衣机, L = 0 * 2 - 0 = 0, R = 2 * 2 - 5= -1, result = 1;

对于第2台洗衣机, L = 1 * 2 - 1= 1, R = 1 * 2 - 5 = -3, result = 3;

对于第3台洗衣机, L = 2 * 2 - 1= 3, R = 0 * 2 - 0= 0, result = 3。

因此,最小必要移动次数就是3。很巧妙有木有?

代码

class Solution {
public:
    int findMinMoves(vector<int>& machines) {
        int len = machines.size();
        vector<int> sum(len + 1, 0);
        for (int i = 0; i < len; ++i) {
            sum[i + 1] = sum[i] + machines[i];
        }
        if (sum[len] % len) {
            return -1;
        }
        int avg = sum[len] / len, res = 0;
        for (int i = 0; i < len; ++i) {     // for each washing machine
            int l = i * avg - sum[i];       // left side has i machines, and right side has (len - i - 1) machines
            int r = (len - i - 1) * avg - (sum[len] - sum[i] - machines[i]);
            if (l > 0 && r > 0) {
                res = std::max(res, std::abs(l) + std::abs(r));
            }
            else {
                res = std::max(res, std::max(std::abs(l), std::abs(r)));
            }
        }
        return res;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值