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

这道题题给了我们一堆工藤新一,噢不,是滚筒洗衣机。我们有许多洗衣机,每个洗衣机里的衣服数不同,每个洗衣机每次只允许向相邻的洗衣机转移一件衣服,问要多少次才能使所有洗衣机的衣服数相等。注意这里的一次移动是说所有洗衣机都可以移动一件衣服到其相邻的洗衣机。这道题的代码量其实不多,难点是在于解题思路,难的是对问题的等价转换等。博主也没有做出这道题,博主想到了要先验证衣服总数是否能整除洗衣机的数量,然后计算出每个洗衣机最终应该放的衣服数,返回跟初始状态衣服数之差的最大值,但这种解法是不对的,无法通过这个test case [0, 0, 11, 5],最终每个洗衣机会留4件衣服,我想的那方法会返回7,然后正确答案是8。想想也是,如果这么是这么简单的思路,这题怎么会标记为Hard呢,还是图样图森破啊。这里直接参照大神Chidong的帖子来做,我们就用上面那个例子,有四个洗衣机,装的衣服数为[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次,参见代码如下:

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

参考资料:

https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution

https://discuss.leetcode.com/topic/79923/c-16ms-o-n-solution-with-trivial-proof

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Super Washing Machines 超级洗衣机的更多相关文章

  1. 517 Super Washing Machines 超级洗衣机

    详见:https://leetcode.com/problems/super-washing-machines/description/ C++: class Solution { public: i ...

  2. [Swift]LeetCode517. 超级洗衣机 | Super Washing Machines

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

  3. Leetcode - 517 Super Washing Machines

    今天开始定期记录本人在leetcode上刷题时遇到的有意思的题目.   517. Super Washing Machines   You have n super washing machines ...

  4. LeetCode517. Super Washing Machines

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

  5. 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心

    Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的 ...

  6. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  7. 517. Super Washing Machines

    ▶ 超级洗碗机.给定一个有 n 元素的整数数组,我们把 “将指定位置上元素的值减 1,同时其左侧或者右侧相邻元素的值加 1” 称为一次操作,每个回合内,可以选定任意 1 至 n 个位置进行独立的操作, ...

  8. Leetcode 517.超级洗衣机

    超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将每台洗衣机 ...

  9. Java实现 LeetCode 517 超级洗衣机

    517. 超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将 ...

随机推荐

  1. Tomcat优化内存以及连接数

    公司的一个服务器使用Tomcat6默认配置,在后台一阵全点击服务器就报废了,查了一下就要是PERMSIZE默认值过小造成(16-64) TOMCAT_HOME/bin/catalina.sh 添加一行 ...

  2. 以太坊开发DApp入门教程——区块链投票系统(一)

    概述 对初学者,首先要了解以太坊开发相关的基本概念.   学习以太坊开发的一般前序知识要求,最好对以下技术已经有一些基本了解: 一种面向对象的开发语言,例如:Python,Ruby,Java... 前 ...

  3. Java字符编码浅析

    Java基本类型占用的字节数:1字节: byte , boolean2字节: short , char4字节: int , float8字节: long , double注:1字节(byte)=8位( ...

  4. beta冲刺2

    q前言:今天晚上大概把github的的相关东西弄上了.然后把之前做的一些修改什么的也上传上去了.目测之后的话就是在自己这边改完然后直接上github那边去复制粘贴替换掉了. 昨天的问题:github这 ...

  5. 解决flask的端口占用

    问题:socket.error: [Errno 48] Address already in use 在编辑flask代码时,如果没有关闭flask的程序,默认的5000端口一直被占用. 再次运行fl ...

  6. 局域网下访问其他计算机搭建的django网页

    1.修改工程目录下的setting.py 文件 ALLOWED_HOSTS = ['*'] #*表示允许访问的ip 如果是添加* 则允许所有同局域网环境的主机访问 2.在完成的django工程下运行以 ...

  7. python的测试

    测试 知识点 单元测试概念 使用 unittest 模块 测试用例的编写 异常测试 测试覆盖率概念 使用 coverage 模块 实验步骤 1. 应该测试什么? 如果可能的话,代码库中的所有代码都要测 ...

  8. 使用C#开发Android应用之WebApp

    近段时间了解了一下VS2017开发安卓应用的一些技术,特地把C#开发WebApp的一些过程记录下来, 欢迎大家一起指教.讨论,废话少说,是时候开始表演真正的技术了.. 1.新建空白Android应用 ...

  9. EasyUI 修改 Messager 消息框大小

    需求是要修改确认消息窗口的大小. 简单的调用方法是这样的: $.messager.confirm('操作确认', '确定批量编辑文章?', function (r) { ... } 这个时候生成的弹窗 ...

  10. 【52ABP实战教程】0.2-- VSTS中的账号迁移到东亚

    需求从哪里来! VSTS的全称是Visual Studio Team Services. 在上一篇的文章中已经给大家说了VSTS之前是没有香港节点.大家的访问速度回比较慢.但是11月10号微软就宣布开 ...