[LeetCode] 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:
- The range of n is [1, 10000].
- The range of dresses number in a super washing machine is [0, 1e5].
這道題題給了我們一堆工藤新一,噢不,是滾筒洗衣機。我們有許多洗衣機,每個洗衣機裡的衣服數不同,每個洗衣機每次只允許向相鄰的洗衣機轉移一件衣服,問要多少次才能使所有洗衣機的衣服數相等。注意這裡的一次移動是說所有洗衣機都可以移動一件衣服到其相鄰的洗衣機。這道題的程式碼量其實不多,難點是在於解題思路,難的是對問題的等價轉換等。博主也沒有做出這道題,博主想到了要先驗證衣服總數是否能整除洗衣機的數量,然後計算出每個洗衣機最終應該放的衣服數,返回跟初始狀態衣服數之差的最大值,但這種解法是不對的,無法通過這個test case [0, 0, 11, 5],最終每個洗衣機會留4件衣服,我想的那方法會返回7,然後正確答案是8。想想也是,如果這麼是這麼簡單的思路,這題怎麼會標記為Hard呢,還是圖樣圖森破啊。這裡直接參照
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; } };
參考資料: