Problem:

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]

Summary:

对于一个长度为n的整型数数组,每步将n-1个元素加1,求最少需要多少步,能使数组中的数字全部相同。

Analysis:

若使数组尽快满足题目要求,则需要每次给除去最大值的其余数字加1,但这种方法效率过低,可以换一种思路。每次将数组中的n-1个数字加1,相当于将剩余的一个数字减1。所以只需找到数组中的最小值m,计算m与数组中其他数字差的累计和即可。

 class Solution {
public:
int minMoves(vector<int>& nums) {
sort(nums.begin(), nums.end());
int len = nums.size(), res = ; for (int i = ; i < len; i++) {
res += nums[i] - nums[];
} return res;
}
};

LeetCode 453 Minimum Moves to Equal Array Elements的更多相关文章

  1. 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 ...

  2. 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 ...

  3. LeetCode: 453 Minimum Moves to Equal Array Elements(easy)

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

  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. ajax浅析---UpdatePanel

    使用UpdatePanel控件 UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何 ...

  2. 关于转录组比对STAR软件使用

    参考文章:http://weibo.com/p/23041883f77c940102vbkd?sudaref=passport.weibo.com 软件连接:https://github.com/al ...

  3. Ruby基本语法规则

    1.Ruby常用数据类型 Numbers, Strings, Booleans my_num = 25 my_boollean = true (or false) my_string = " ...

  4. eclipse工具背景色模板-程序员保护好自己的眼睛

    做为coder,要保护好自己的眼睛,eclipse 强烈推荐 Eclipse Color Theme插件,该插件包含多种当前流行的主题选择. 安装方法: 安装方法:1.先安装一个Eclipse Col ...

  5. Handler Should be static or leaks Occur?

    解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...

  6. Jquery中的事件和动画

    在学习Jquery中的过程中我们绝大部分都用到了事件的操作,也可以说事件是Jquery中必不可少的一部分,我们常见的一些事件有单击事件,鼠标事件,键盘事件等等.在Jquery中的学习中为了能使让页面以 ...

  7. codeblocks+Mingw 下配置开源c++单元测试工具 google test

    google test 是google的c++开源单元测试工具,chrome的开发团队就是使用它. Code::Blocks 12.11(MinGW 4.7.1) (Windows版)Google T ...

  8. rem和em,px的使用

    rem是CSS3中新增加的一个单位值,他和em单位一样,都是一个相对单位.不同的是em是相对于元素的父元素的font-size进行计算:rem是相对于根元素html的font-size进行计算.这样一 ...

  9. Redis学习笔记三:多机数据库的实现

    1.复制 执行slaveof命令或者设置slaveof选项,让一个服务器去复制另外一个服务器. 旧版复制功能的实现(Redis 2.8 之前的版本) 复制功能分为同步和命令传播两个操作. 同步(syn ...

  10. Units Problem: How to read text size as custom attr from xml and set it to TextView in java code

    Here is this topic’s background: I defined a custom View which extends FrameLayout and contains a Te ...