题目:

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

Example:

Input:
[1,2,3] Output:
3 Explanation:
Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]

代码:

这道题给了我们一个长度为n的数组,说是我们每次可以对n-1个数字同时加1,问最少需要多少次这样的操作才能让数组中所有的数字相等。那么我们想,为了快速的缩小差距,该选择哪些数字加1呢,不难看出每次需要给除了数组最大值的所有数字加1,这样能快速的到达平衡状态。但是这道题如果我们老老实实的每次找出最大值,然后给其他数字加1,再判断是否平衡,思路是正确,但是OJ不答应。

正确的解法相当的巧妙,需要换一个角度来看问题,其实给n-1个数字加1,效果等同于给那个未被选中的数字减1,比如数组[1,2,3], 给除去最大值的其他数字加1,变为[2,3,3],我们全体减1,并不影响数字间相对差异,变为[1,2,2],这个结果其实就是原始数组的最大值3自减1,那么问题也可能转化为,将所有数字都减小到最小值,这样难度就大大降低了,我们只要先找到最小值,然后累加每个数跟最小值之间的差值即可,参见代码如下:

 class Solution {
public:
int minMoves(vector<int>& nums) {
int mn = INT_MAX, res = ;
for (int num : nums) mn = min(mn, num);
for (int num : nums) res += num - mn;
return res;
}
};

LeetCode: 453 Minimum Moves to Equal Array Elements(easy)的更多相关文章

  1. LeetCode 453 Minimum Moves to Equal Array Elements

    Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...

  2. LeetCode 453. Minimum Moves to Equal Array Elements C#

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  3. 13. leetcode 453. Minimum Moves to Equal Array Elements

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  4. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  5. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  6. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  7. [LeetCode&Python] Problem 453. Minimum Moves to Equal Array Elements

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  8. 453. Minimum Moves to Equal Array Elements

    Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...

  9. 453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等

    给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1.示例:输入:[1,2,3]输出:3解释:只需要3次移动(注意每次移动会增加两个元素 ...

随机推荐

  1. UESTC 1061 秋实大哥与战争 线段树区间合并

    秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 男儿何不带吴钩, ...

  2. 访问虚拟机中的架设的Web服务器

    环境: 1.虚拟机中安装了CentOS,虚拟机使用NAT的方式 2.在CentOS中安装了APACHE 并且使用 http://127.0.0.1可以正常访问,通过ifconfig查到IP地址是 19 ...

  3. UI层自动化测试介绍

    UI指的是用户可以用肉眼可以看到的页面. UI层自动化测试的原理.不论是web端还是移动端,原理都是一样的,就是基于页面元素的识别和定位来进行模拟用户行为. 首先识别到某个元素,比如一个按钮,然后定义 ...

  4. Tomcat_异常_01_Setting property 'source' to 'org.eclipse.jst.jee.server:AWeiXin_QiYe_Demo' did not find a matching property

    关于解决方法: 1.解决Setting property 'source' to 'org.eclipse.jst.jee.server的问题 2.eclipse中server location为灰色 ...

  5. 尴尬的app:layout_scrollFlags="scroll|enterAlways" 配合NavigationDrawer

    昨天想到了NavigationDrawer中Item点击的问题. 点击Drawer中的一个Item需要到一个新的页面,你是应该打开一个新的Activity呢还是直接用fragment呢? 如果打开新的 ...

  6. Ubuntu 16.10 中文环境 Shell输出英文提示

    /********************************************************************************** * Ubuntu 16.10 中 ...

  7. BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6371  Solved: 2496[Submit][Statu ...

  8. 51nod 1250 排列与交换——dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1250 仔细思考dp. 第一问,考虑已知 i-1 个数有多少种方案. ...

  9. protocol 和 delegate

    步骤 1.发出协议(在发协议者.h文件 下@interface-@end 上边) @protocol hireOneMaid <NSObject> @required//必须要实现的方法( ...

  10. 删除老的Azure Blob Snapshot

    客户有这样的需求:每天需要对VM的数据进行备份,但如果备份的时间超过一定的天数,需要进行清除. 本文也是在前一篇Azure Blob Snapshot上的优化. "Azure blob St ...