You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.html
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 .java
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.数组
Example1post
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
Example2url
Input: [0,3,0] Output: 2 Explanation: 1st move: 0 <-- 3 0 => 1 2 0 2nd move: 1 2 --> 0 => 1 1 1
Example3spa
Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses.
Note:code
这道题题给了咱们一堆工藤新一,噢不,是滚筒洗衣机。咱们有许多洗衣机,每一个洗衣机里的衣服数不一样,每一个洗衣机每次只容许向相邻的洗衣机转移一件衣服,问要多少次才能使全部洗衣机的衣服数相等。注意这里的一次移动是说全部洗衣机均可以移动一件衣服到其相邻的洗衣机。这道题的代码量其实很少,难点是在于解题思路,难的是对问题的等价转换等。博主也没有作出这道题,博主想到了要先验证衣服总数是否能整除洗衣机的数量,而后计算出每一个洗衣机最终应该放的衣服数,返回跟初始状态衣服数之差的最大值,但这种解法是不对的,没法经过这个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次,参见代码以下:htm
class Solution { public: int findMinMoves(vector<int>& machines) { int sum = accumulate(machines.begin(), machines.end(), 0); if (sum % machines.size() != 0) return -1; int res = 0, cnt = 0, avg = sum / machines.size(); for (int m : machines) { cnt += m - avg; res = max(res, max(abs(cnt), m - avg)); } return res; } };
参考资料:blog
https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solutionthree
https://discuss.leetcode.com/topic/79923/c-16ms-o-n-solution-with-trivial-proof