[leetcode]517. Super Washing Machines
来源:互联网 发布:人工智能家庭生活 编辑:程序博客网 时间:2024/06/05 21:02
题目链接:https://leetcode.com/problems/super-washing-machines/#/description
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: 3Explanation: 1st move: 1 0 <-- 5 => 1 1 42nd move: 1 <-- 1 <-- 4 => 2 1 3 3rd move: 2 1 <-- 3 => 2 2 2
Example2
Input: [0,3,0]Output: 2Explanation: 1st move: 0 <-- 3 0 => 1 2 0 2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]Output: -1Explanation: 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].
First. we determine the target dresses we should have in each machine (total dresses / # of machines), and return -1 if it's not an integer.
We then go from left to right, tracking the running balance of dresses we need to move through each machine. If, for example, we have 5 extra dresses so far, and this machine has 2 extra dresses, we need to pass total 7 dresses through that machine (requires 7 steps). Also, we need to track the number of dresses we need to offload from a particular machine (machine[i] - target dresses). This number may be higher than the running balance if dresses are passed both ways, as shown in the example 2.
Example 1: [1, 1, 6, 6, 1], total dresses: 15, target dresses: 3, maximum offload is 3 (6 - 3).
Running balance:[-2][-4][ -1][ 2][ 0]
Answer: max(3, abs(-4)) = 4.
Example 2: [1, 1, 4, 8, 1], total dresses: 15, target dresses: 3, maximum offload is 5 (8 - 3).
Running balance:[-2][-4][ -3][ 2][ 0]
Answer: max(5, abs(-4)) = 5
class Solution {public: int findMinMoves(vector<int>& machines) { int totalDresses=0,size=machines.size(); for(int i=0;i<size;i++) totalDresses+=machines[i]; if(totalDresses%size!=0) return -1; auto targetDresses=totalDresses/size,totalMoves=0,balance=0; for(int i=0;i<size;i++) { balance+=machines[i]-targetDresses; totalMoves=max(totalMoves,max(machines[i]-targetDresses,abs(balance))); } return totalMoves; }};
- 【Leetcode】517. Super Washing Machines
- [leetcode] 517. Super Washing Machines
- LeetCode 517. Super Washing Machines
- LeetCode 517. Super Washing Machines
- 【LeetCode】517. Super Washing Machines
- [leetcode]517. Super Washing Machines
- Leetcode 517. Super Washing Machines
- LeetCode 517. Super Washing Machines
- [leetcode]Super Washing Machines
- [Leetcode] 517. Super Washing Machines 解题报告
- leetcode 517. Super Washing Machines 超级洗衣机
- 517.Super Washing Machines
- 517.Super Washing Machines
- 517. Super Washing Machines
- 517. Super Washing Machines
- [LeetCode OJ]Super Washing Machines
- LeetCode[517] Super Washing Machines
- LeetCode 517 Super Washing Machines
- leetcode 132 Pattern
- 面向对象的几个问题
- Array Partition I
- Spring Cloud构建微服务架构:服务消费(Ribbon)【Dalston版】
- MAMP PRO的mySQL启动失败
- [leetcode]517. Super Washing Machines
- 1049. 数列的片段和(20)
- 冒泡事件、默认事件、事件委托、事件监听和事件捕获
- sqlserver、Mysql、Oracle三种数据库的优缺点总结
- ELF文件格式及其他
- Activity的启动模式(一)
- javascript 获取元素在父节点中的下标
- ssh框架
- myeclipse基本操作