LintCode 1194: Super Washing Machines (一道非常好的题,并行处理)

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

Example
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.
Notice
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].

这道题目设计得非常好。思路我感觉不容易想到。我参考了
https://blog.csdn.net/tstsugeg/article/details/62427718

先预处理得到每个机器i的sums[i]。
然后根据每个机器i,算出它的左侧和右侧的净增益(不包括它自己)gainL和gainR。
分3种情况:

  1. gainL > 0 && gainR > 0
    这种情况说明左右两侧都净增益>0,都必须往机器i转移衣服: 左侧转移gainL,右侧转移gainR,机器i最多操作为max(gainL, gainR) 。注意左右两侧可并行处理。
  2. gainL < 0 && gainR < 0
    这种情况说明左右两侧都净增益<0, 机器i必须往左右两侧转移衣服: 往左侧转移gainL,往右侧转移gainR。因为不能并行处理,机器i最多操作为-gainL-gainR。
  3. gainL <= 0 && gainR >= 0 or gainL >= 0 && gainR <= 0
    这种情况最复杂。它说明左右两侧有一侧净增益>=0,另一侧净增益<=0。同时机器i本身的净增益即为两侧之差。即假如左侧净增益为-5,右侧为+2,则机器i本身净增益为-3。那么机器i所需操作最多为5,因为右侧需要往机器i和机器i的左侧输送5件衣服。所以机器i的操作为max(abs(gainL), abs(gainR))。

我们遍历所有机器,因为所有机器可以并行操作,取操作数的最大值即为题目所求的最小操作数。

class Solution {
public:
    /**
     * @param machines: an integer array 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
     */
    int findMinMoves(vector<int> &machines) {
        int len = machines.size();
        vector<int> sums(len, 0);
        
        int averageNum = 0;
        
        sums[0] = machines[0];
        
        for (int i = 1; i < len; ++i) {
            sums[i] = sums[i - 1] + machines[i];
        }
    
        averageNum = sums[len - 1] / len;
        if (sums[len - 1] != averageNum * len) return -1;

        int optSteps = INT_MIN;
        for (int i = 0; i < len; ++i) {
            
            int gainL, gainR;
            
            gainL = (i == 0) ? 0 : sums[i - 1] - averageNum * i;
            gainR = (i == len - 1) ? 0 : sums[len - 1] - sums[i] - averageNum * (len - 1 - i); 
            
            if (gainL > 0 && gainR > 0) {
                //machine i needs to input clothes from both left side and right side, choose max as both sides can do in parallel 
                optSteps = max(optSteps, max(gainL, gainR)); 
            } else if (gainL < 0 && gainR < 0) {
                //machine  i needs to output clothes to both left side and right side. It cannot be done in parallel, so use the sum
                optSteps = max(optSteps, -gainL - gainR);
            } else {
                //machine i's one siide needs to transfer clothes to the other side, so use minus
                optSteps = max(optSteps, max(abs(gainL), abs(gainR)));
            }
        }
     
        return optSteps;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值