问题描述:

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     4
2nd 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:

  1. The range of n is [1, 10000].
  2. The range of dresses number in a super washing machine is [0, 1e5].

问题分析:这是状态转移问题,每个洗衣机在同一时间只能向相邻洗衣机传递一件衣服,则合法的转移操作是在从左向右的+1、-1变化必须成对出现,例如:-1,0,+1,+1,0,0,0,-1,而这种-1,-1,0,0,+1,-1,0,+1,+1,+1是不合法的转移操作。而对于k个所有合法操作进行累加和,必然是需要转移的每个洗衣机与最终平衡洗衣机衣服数目的差值。对于合法操作的特点:从左向右进行累加,最大值必然是1,所以对于所有k个合法操作累加之后的合法操作同样进行从左向右累加,最大值必然是k,即转移操作的次数。由于每个合法操作的最大值出现的位置不同,从左向右累加和必须和当前洗衣机本身需要转移的衣服数目进行比较。

public int findMinMoves(int[] machines) {int n=machines.length;if(n<0)return -1;int sum=0;for(int i=0;i<n;i++)sum+=machines[i];if(sum%n!=0)return -1;int avg=sum/n;int leftnum=0;int minmove=0;for(int i=0;i<n;i++){int cu=machines[i]-avg;leftnum+=cu;minmove=Math.max(minmove,Math.max(Math.abs(leftnum),cu));}return minmove;}

参考链接:
http://blog.csdn.net/ljytfsto/article/details/60757973
http://blog.csdn.net/tstsugeg/article/details/62427718

Leetcode 517. Super Washing Machines相关推荐

  1. [leetcode] 517. Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  2. leetcode 517. Super Washing Machines 超级洗衣机 + 传递衣服 + 发现规律

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  3. Leetcode 517. Super Washing Machines [Python]

    最后每个洗衣机都会得到均值.所以,对于单个的洗衣机,移动衣服的次数最小是均值减去洗衣机里的衣服数.同样,对于一组相邻的洗衣机,总的移动次数也最小是均值*洗衣机数字 减去 这堆洗衣机里的衣服的总数. 题 ...

  4. 517.Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  5. 517. Super Washing Machines(超级洗衣机)

    问题描述 You have n super washing machines on a line. Initially, each washing machine has some dresses o ...

  6. LeetCode——517. 超级洗衣机(Super Washing Machines)[困难]——分析及代码(C++)

    LeetCode--517. 超级洗衣机[Super Washing Machines][困难]--分析及代码[C++] 一.题目 二.分析及代码 1. 贪心 (1)思路 (2)代码 (3)结果 三. ...

  7. 动态规划:Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  8. Java实现 LeetCode 517 超级洗衣机

    517. 超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将 ...

  9. LeetCode 517 超级洗衣机 解法

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/super-washing-machines 著作权归领扣网络所有.商业转载请联系官方授权,非商 ...

最新文章

  1. 如何使用eclipse创建Javaweb项目
  2. Xcode 新项目删除 storyboard 文件
  3. 照片识别出错_AI跨年龄人脸识别技术在跨年龄寻亲的应用简析
  4. ECSHOP模板设置只有登录用户才能查看商品详情内容
  5. 7-3 二叉搜索树的结构 (30 分)
  6. Visual Studio Code(VS code)简单使用入门以及常用快捷键
  7. MyEclipse 10 破解
  8. linux 主机支持远程唤醒_在Linux下用Wake On LAN实现远程开机
  9. IDM统一认证功能说明
  10. 阿兹卡塔防 Azgard Defense 详细攻略
  11. php英文星期中文星期,英文星期到星期天【星期一到星期天的英文用中文怎么说。发音标准的来。】...
  12. RSTP快速生成树简介、RSTP与STP的区别、RSTP BPDU字段信息、RSTP角色与端口状态介绍、RSTP工作过程之P/A机制详细分解)
  13. 【CodingNoBorder - 07】无际软工队 - 求职岛:ALPHA 阶段测试报告
  14. 8.0服务器维护时间,魔兽世界8.0开服第一天遇紧急维护?网友:叫逆水寒还服务器...
  15. 沅有芷兮:类型系统的数学之美
  16. Linux软件防火墙:iptables
  17. 财务分析与决策:同型分析
  18. linux如何配置php环境,linux如何配置php环境
  19. (20200911Solved)WSL: 在配置文件列表中找不到你的默认配置文件...
  20. 【智能家居市场】华为hilink

热门文章

  1. 购买Blender cloud支援今年官方开源电影Gooseberry
  2. win10硬盘锁怎么解除_如何取消硬盘锁?
  3. linux系统盘锁着,用Linux启动盘解决硬盘逻辑锁
  4. 笔记本电脑无法搜索到wifi
  5. POJ3608-旋转卡壳
  6. 老九学堂-删除重复值
  7. FuntouchOS跟鸿蒙系统,FuntouchOS10.5安装包
  8. 计算机电路计数器pl什么意思,计数器的原理为什么1下来是2.而且频率是一样的.它是怎么进位的.它的电路原理是什么...
  9. Simple java里面的paint(Graphics g)的理解
  10. CCF 201604-2 俄罗斯方块