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 .

(注:每次可以选择任意多的机器,但是每次只能移动每个机器上的一个dress,并且只能传递到它的一个相邻machine中)

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.

分析

首先可以根据dress的总数目来确定最后每个machine上的dress,如果不能整除则返回-1.

如果有解的话,我们总是能够将一个dress从一个machine传给另一个machine,直到每个machine上的dress数量相同。

Since we can operate several machines at the same time, the minium number of moves is the maximum number of necessary operations on every machine.

For a single machine, necessary operations is to transfer dresses from one side to another until sum of both sides and itself reaches the average number. We can calculate (required dresses) - (contained dresses) of each side as L and R:

L > 0 && R > 0: both sides lacks dresses, and we can only export one dress from current machines at a time, so result is abs(L) + abs(R)   
L < 0 && R < 0: both sides contains too many dresses, and we can import dresses from both sides at the same time, so result is max(abs(L), abs(R))
L < 0 && R > 0 or L >0 && R < 0: the side with a larger absolute value will import/export its extra dresses from/to current machine or other side, so result is max(abs(L), abs(R))

For example, [1, 0, 5], average is 2
for 1, L = 0 * 2 - 0 = 0, R = 2 * 2 - 5= -1, result = 1  //对于第一个元素1,因为它左边没有元素所以需要的dress是0,目前所包含的dress是0。而他右边有两台机器,所以需要的dress是2*2=4,目前所拥有的dress是0+5=5.
for 0, L = 1 * 2 - 1= 1, R = 1 * 2 - 5 = -3, result = 3
for 5, L = 2 * 2 - 1= 3, R = 0 * 2 - 0= 0, result = 3

代码

class Solution {
public int findMinMoves(int[] machines) {
int len = machines.length;
int[] sum = new int[machines.length + 1];
for (int i = 0; i < len; i++)
sum[i + 1] = sum[i] + machines[i]; if (sum[len] % len != 0) return -1; int avg = sum[len] / len;
int res = 0;
for (int i = 0; i < len; ++i) {
int l = i * avg - sum[i];
int r = (len - i - 1) * avg - (sum[len] - sum[i] - machines[i]);
if (l > 0 && r > 0)
res = Math.max(res, Math.abs(l) + Math.abs(r));
else {
res = Math.max(res, Math.max(l, r));
}
}
return res;
}
}

其实还是有点不是很明白这么解的原理,也有另一种方法https://www.cnblogs.com/grandyang/p/6648557.html,更加简单点,但是还是没搞清除背后的数学原理是什么。

LeetCode517. Super Washing Machines的更多相关文章

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

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

  2. [LeetCode] 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. 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心

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

  5. 517. Super Washing Machines

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

  6. 517 Super Washing Machines 超级洗衣机

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

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. Leetcode 763. Partition Labels

    思路:动态规划.对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1:如果amount-co ...

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

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. libcurl代码示例

    http://note.youdao.com/noteshare?id=e60a679d1731c870ff45e403de81a2c0

  2. bzoj 1798 线段树

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 7163  Solved: 2587[Submit ...

  3. Java基础-线程操作共享数据的安全问题

    Java基础-线程操作共享数据的安全问题 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.引发线程安全问题 如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运 ...

  4. Android 6.0 7.0 8.0 一个简单的app内更新版本-okgo app版本更新

    登陆时splash初始页调用接口检查app版本.如有更新,使用okGo的文件下载,保存到指定位置,调用Android安装apk. <!-- Android 8.0 (Android O)为了针对 ...

  5. bzoj千题计划109:bzoj1019: [SHOI2008]汉诺塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1019 题目中问步骤数,没说最少 可以大胆猜测移动方案唯一 (真的是唯一但不会证) 设f[i][j] ...

  6. A Gentle Guide to Machine Learning

    A Gentle Guide to Machine Learning Machine Learning is a subfield within Artificial Intelligence tha ...

  7. 把一个IEEE754浮点数转换为IBM370浮点数的C#代码

    把一个IEEE754浮点数转换为IBM370浮点数的C#代码. 在这个网页上有古老的IBM370浮点格式的说明. // http://en.wikipedia.org/wiki/IBM_Floatin ...

  8. Java并发编程原理与实战一:聊聊并发

    一.大纲 •你真的了解并发吗 •多线程和并发 •多线程和多进程 •线程一定快吗 •学习并发的四个阶段 •学习目标 •适合人群 •荐书   二.学习并发的四个阶段 •熟练掌握API,能够完成并发编程 • ...

  9. idea注册码激活防和谐

    1.到网站 http://idea.lanyus.com/ 获取注册码: 2.修改hosts文件,位于C:\Windows\System32\drivers\etc,添加一行,win10推荐使用not ...

  10. 自定义模板类型vs模板类型自动推测

    [自定义模板类型vs模板类型自动推测] GCC在编译时,先确定看是否人工定义了模板的类型,如果定义了,则看传递的参数是否能转换成定义后的类型,如果没定义,则根据参数确定定义的类型. 1. 是否人工定义 ...