LeetCode517. Super Washing Machines

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

(注:每次能夠選擇任意多的機器,可是每次只能移動每一個機器上的一個dress,而且只能傳遞到它的一個相鄰machine中)ui

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

Example1code

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   

Example2htm

Input: [0,3,0]

Output: 2

Explanation: 
1st move:    0 <-- 3     0    =>    1     2     0    
2nd move:    1     2 --> 0    =>    1     1     1     

Example3blog

Input: [0,2,0]

Output: -1

Explanation: 
It's impossible to make all the three washing machines have the same number of dresses. 

分析three

首先能夠根據dress的總數目來肯定最後每一個machine上的dress,若是不能整除則返回-1.get

若是有解的話,咱們老是可以將一個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; } }

其實仍是有點不是很明白這麼解的原理,也有另外一種方法http://www.javashuo.com/article/p-xnkjnkwa-bg.html,更加簡單點,可是仍是沒搞清除背後的數學原理是什麼。

相關文章
相關標籤/搜索