[leetcode] 517. Super Washing Machines

441 篇文章 0 订阅
284 篇文章 0 订阅

Description

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].

分析

题目的意思是:给定一些一洗衣机,然后求出最小的步数来使得每个洗衣机的衣服数量相同。

  • 假设有四个洗衣机,装的衣服数为[0, 0, 11, 5],最终的状态会变为[4, 4, 4, 4],那么将二者做差,得到[-4, -4, 7, 1],这里负数表示当前洗衣机还需要的衣服数,正数表示当前洗衣机多余的衣服数。要做的是要将这个差值数组每一项都变为0,对于第一个洗衣机来说,需要四件衣服可以从第二个洗衣机获得,那么就可以把-4移给二号洗衣机,那么差值数组变为[0, -8, 7, 1],此时二号洗衣机需要八件衣服,那么至少需要移动8次。
  • 然后二号洗衣机把这八件衣服从三号洗衣机处获得,那么差值数组变为[0, 0, -1, 1],此时三号洗衣机还缺1件,就从四号洗衣机处获得,此时差值数组成功变为了[0, 0, 0, 0],成功。那么移动的最大次数就是差值数组中出现的绝对值最大的数字,8次

我也解释不了这个,参见参考2,里面讲得还不错:

  • 对位置k上的洗衣机来说,如果左边k个洗衣机中(下标从0开始)原有衣服总数小于avg*k,表明左边k个洗衣机作为整体最终需要从右边洗衣机(包含位置k)中获取衣服,而获取衣服必定需要通过位置k的洗衣机,右边同理。这里拿lCnt表示位置k左边所有洗衣机最终向右边洗衣机(包含位置k)输送的衣服数,如果lCnt小于0,表示左边洗衣机最终需要从右边洗衣机中获取衣服,同理拿rCnt表示位置k右边所有洗衣机最终向左边洗衣机(包含位置k)中输送的衣服数。lCnt和rCnt在知道了avg之后很容易计算,这样通过判断lCnt和rCnt的正负即可得出位置k上洗衣机的最小操作数。

  • 如果lCnt>0 && rCnt>0
    ​表明位置k需要同时从两侧获取衣服,两侧可以同时进行,所以位置k上最小操作数为max(lCnt, rCnt);

  • 如果lCnt<0 && rCnt<0
    表明位置k同时向两侧输出衣服,两侧不能同时进行,所以位置k上最小操作数为-lCnt-rCnt;

  • 其他情况

  • 表明位置k需要从一侧获取衣服,然后向另一侧输出衣服,两侧可以同时进行,所以位置k上最小操作数为max(abs(lCnt), abs(rCnt))。

代码

class Solution {
public:
    int findMinMoves(vector<int>& machines) {
        int n=machines.size();
        int sum=0;
        for(int num:machines){
            sum+=num;
        }
        if(sum%n!=0){
            return -1;
        }
        int res=0;
        int avg=sum/n;
        int cnt=0;
        for(int m:machines){
            cnt+=m-avg;
            res=max(res,max(abs(cnt),m-avg));
        }
        return res;
    }
};

参考文献

[LeetCode] Super Washing Machines 超级洗衣机
[leetcode] 517. Super Washing Machines
517. Super Washing Machines

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值