给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动可以使 n - 1 个元素增加 1。
示例:
输入:
[1,2,3]
输出:
3
解释:
只需要3次移动(注意每次移动会增加两个元素的值):
[1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]
详见:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/

C++:

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

453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等的更多相关文章

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

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

  2. [LeetCode] 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

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

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

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

  6. 453. Minimum Moves to Equal Array Elements

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

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

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

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

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

随机推荐

  1. 【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】

    [066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a non-negative number represented as ...

  2. 理解Paxos Made Practical

    Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...

  3. VMware一些使用心得

    这段时间VMware workstation用得较多,装了好几个虚拟机,有win2003,win2008,win7,还分32位,64位.装了这么多,要么是用于安装一些软件,比如oracle12c,因为 ...

  4. String StringBuffer StringBuilder 对比

      1.StringBuffer是线程安全的,StringBuilder是非线程安全的   2.对String的修改其实是new了一个StringBuilder并调用append方法,然后调用toSt ...

  5. RJ45接口定义及网线线序

    RJ45接口定义 常见的RJ45接口有两类:用于以太网网卡.路由器以太网接口等的DTE类型,还有用于交换机等的DCE类型. DTE我们可以称做“数据终端设备”,DCE我们可以称做“数据通信设备”.从某 ...

  6. Linux __setup解析【转】

    本文转载自:http://blog.csdn.net/fdaopeng/article/details/7895037 __setup这条宏在Linux Kernel中使用最多的地方就是定义处理Ker ...

  7. return value, output parameter,

    Return Value https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=s ...

  8. Struts2中怎样配置struts。xml?

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...

  9. 获取Android APK JNI库

    /************************************************************************** * 获取Android APK JNI库 * 说 ...

  10. Robot Framework常用的操作库列表

    标准库是Robot Framework可以直接导入使用的库,包含以下几类: Builtin:包含经常需要的关键字.自动导入无需import,因此总是可用的 Dialogs:提供了暂停测试执行和从用户的 ...