[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呢,还是图样图森破啊。这里直接参照大神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次,参见代码如下:
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int sum = accumulate(machines.begin(), machines.end(), );
if (sum % machines.size() != ) return -;
int res = , cnt = , avg = sum / machines.size();
for (int m : machines) {
cnt += m - avg;
res = max(res, max(abs(cnt), m - avg));
}
return res;
}
};
参考资料:
https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution
https://discuss.leetcode.com/topic/79923/c-16ms-o-n-solution-with-trivial-proof
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Super Washing Machines 超级洗衣机的更多相关文章
- 517 Super Washing Machines 超级洗衣机
详见:https://leetcode.com/problems/super-washing-machines/description/ C++: class Solution { public: i ...
- [Swift]LeetCode517. 超级洗衣机 | Super Washing Machines
You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...
- Leetcode - 517 Super Washing Machines
今天开始定期记录本人在leetcode上刷题时遇到的有意思的题目. 517. Super Washing Machines You have n super washing machines ...
- LeetCode517. Super Washing Machines
You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...
- 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心
Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的 ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 517. Super Washing Machines
▶ 超级洗碗机.给定一个有 n 元素的整数数组,我们把 “将指定位置上元素的值减 1,同时其左侧或者右侧相邻元素的值加 1” 称为一次操作,每个回合内,可以选定任意 1 至 n 个位置进行独立的操作, ...
- Leetcode 517.超级洗衣机
超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将每台洗衣机 ...
- Java实现 LeetCode 517 超级洗衣机
517. 超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将 ...
随机推荐
- 【ASP.NET Core】如何隐藏响应头中的 “Kestrel”
全宇宙人民都知道,ASP.NET Core 应用是不依赖服务器组件的,因此它可以独立运行,一般是使用支持跨平台的 Kestrel 服务器(当然,在 Windows 上还可以考虑用 HttpSys,但要 ...
- [poj3252]Round Numbers_数位dp
Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...
- System V IPC 之共享内存
IPC 是进程间通信(Interprocess Communication)的缩写,通常指允许用户态进程执行系列操作的一组机制: 通过信号量与其他进程进行同步 向其他进程发送消息或者从其他进程接收消息 ...
- 循环while do---while for循环
一.循环结构 (.^▽^) 1.循环不是无休止进行的,满足一定条件的时候循环才会继续,称为"循环条件",循环条件不满足的时候,循环退出 2.循环结构是反复进行相同的或类似的一系列操 ...
- JAVA-基础语法篇
JAVA-基础语法篇 一. 基础语法: 对大小写敏感 类名的首字母大写 方法名首字母小写,后面用驼峰发命名 源文件名和类名要相同 主方法入口: public static void main( ...
- spring框架学习笔记4:SpringAOP实现原理
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- C语言函数2
一.PTA实验作业 6-3 使用函数判断完全平方数: 1. 本题PTA提交列表: 2. 设计思路: 3.本题调试过程碰到问题及PTA提交列表情况说明: 1.一开始考虑让输入值N去整除一个循环变量i,i ...
- Beta冲刺随笔集合
Beta冲刺随笔集合 项目Beta预备 Beta冲刺第一天 Beta冲刺第二天 Beta冲刺第三天 Beta冲刺第四天 Beta冲刺第五天 Beta冲刺第六天 Beta冲刺第七天 用户调查报告 Bet ...
- 团队作业2:需求分析&原型设计
Deadline: 2017-11-5 22:00PM,以博客发表日期为准. 评分基准: 按时交 - 有分,检查的项目包括后文的三个方面 需求分析 原型设计 编码规范 晚交 - 0分 迟交两周以 ...
- bzoj千题计划244:bzoj3730: 震波
http://www.lydsy.com/JudgeOnline/problem.php?id=3730 点分树内对每个节点动态维护2颗线段树 线段树以距离为下标,城市的价值为权值 对于节点x的两棵线 ...