LeetCode 517. Super Washing Machines

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

题目内容:
题目给出一个非负整数数组,数组长度为n,表示n台洗衣机,每台洗衣机里面有多少件衣服,我们每次可以从n个洗衣机里面选择m(1<=m<=n)台洗衣机,可以分别从这m台洗衣机取出一件衣服放到其中一台相邻的洗衣机里面。题目让我们求出最少的移动次数,使得每台洗衣机的衣服数量相同,如果不可能则返回-1。

解题思路:
假设总共有sumClothes件衣服,那么首先对于不能使所有洗衣机的衣服数量不相同的情况,只有是 sumClothes%m!=0 这种情况。所以一开始先求出衣服的总数量sumClothes,这可以通过一次迭代完成,时间复杂度为 O(n) 。然后,每台洗衣机应该包含的衣服数量就是

averageClothes=sumClothesn

要求最少的移动次数,可以看成求需要移动次数最多的洗衣机需要移动的次数。对于每一台洗衣机,它所包含的衣服有3种去向:

  1. 留在目前的洗衣机
  2. 移动到左边的洗衣机
  3. 移动到右边的洗衣机啊
    用toLeft表示应该移动到左边的衣服数量,也就是左边的洗衣机需要的衣服数量,toRight则是移动的右边的衣服数量。因为是从左到右遍历这些洗衣机,所以第一台洗衣机的toLeft是0,然后每次迭代计算toRight,第i台洗衣机的toRight其实就是第i+1台洗衣机的toLeft。对于第i台洗衣机,
    toRight=machines[i]averageClothestoLeft

    这个式子可以理解为,将超过平均值的衣服往左移动后,剩下的都往右边移动。
    但是可以发现这里的toLeft和toRight都有可能是负数,其实负数我们只要理解为移动的方向反了而已就可以了。
    假设用result来保存最后的结果,将result初始化为0,那么每一次迭代,result更新为
    result=max(reuslt,toLeft,toRight,toLeft+toRight)

    遍历完所有的洗衣机,就可以得到题目要求的结果。

代码:

//
//  main.cpp
//  517. Super Washing Machines
//
//  Created by mingjc on 2017/4/9.
//  Copyright © 2017年 mingjc. All rights reserved.
//

#include <iostream>
#include <vector>

#define max(a, b) (a > b ? a : b)
using namespace std;

class Solution {
public:
    int findMinMoves(vector<int>& machines) {
        int sumClothes = 0;
        int averageClothes = 0;
        for (int i = 0; i < machines.size(); i++) {
            sumClothes += machines[i];
        }
        averageClothes = sumClothes / float(machines.size());
        if (sumClothes % int(machines.size()) != 0) {
            return -1;
        }

        int toLeft = 0;
        int toRight = 0;
        int result = 0;
        for (int i = 0; i < machines.size(); i++) {
            int toRight = machines[i] - averageClothes - toLeft;
            result = max(result, toRight);
            result = max(result, toLeft);
            result = max(result, toLeft + toRight);

            toLeft = -toRight;
        }
        return result;
    }
};

int main(int argc, const char * argv[]) {
    int array[] = {1, 0, 5};
    int count = sizeof(array) / sizeof(int);
    vector<int> machines(array, array + count);
    Solution sln;
    cout << sln.findMinMoves(machines) << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值