453. 最小移动次数使数组元素相等

453. Minimum Moves to Equal Array Elements

题目描述

给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动可以使 n - 1 个元素增加 1。

示例:

输入:
[1,2,3]

输出:

3

解释:

只需要 3 次移动(注意每次移动会增加两个元素的值):

[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]

每日一算法2019/6/19Day 47LeetCode453. Minimum Moves to Equal Array Elements

Java 实现

class Solution {
public int minMoves(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
for (int num : nums) {
min = Math.min(num, min);
}
int res = 0;
for (int num : nums) {
res += num - min;
}
return res;
}
}

相似题目

参考资料

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

  1. [Swift]LeetCode453. 最小移动次数使数组元素相等 | 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 ...

  2. Java实现 LeetCode 453 最小移动次数使数组元素相等

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

  3. LeetCode#453 最小移动次数使数组元素相等

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

  4. 力扣(LeetCode)453. 最小移动次数使数组元素相等

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

  5. [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二

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

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

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

  7. LeetCode Minimum Moves to Equal Array Elements II

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...

  8. LeetCode Minimum Moves to Equal Array Elements

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...

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

随机推荐

  1. 学习:窗口创建以及消息处理basic.c

    WNDCLASS结构: Windows 的窗口总是基于窗口类来创建的,窗口类同时确定了处理窗口消息的窗口过程(回调函数). 在创建应用程序窗口之前,必须调用 RegisterClass 函数来注册窗口 ...

  2. python - django 解决 templates 模板语言语法冲突

    # 在使用某个框架时发现语法与Django的模板语法冲突了,于是找到解决方案: {% verbatim %} // 被 verbatim 包裹起来的地方是不会被 django 渲染的 {% endve ...

  3. 2019 CSP-J复赛游记

    不出行?不出行考屁呢? 今天的CSP-J似乎比去年简单了一些,可它... 好了,来说一说我的情况. T1:太水,5分钟秒 T2:这个数据有点尴尬,双重循环铁定爆,用链表有有一点小题大做.本蒟蒻在考场上 ...

  4. 信噪比(signal-to-noise ratio)

    SNR或S/N,又称为讯噪比.是指一个电子设备或者电子系统中信号与噪声的比例.这里面的信号指的是来自设备外部需要通过这台设备进行处理的电子信号,噪声是指经过该设备后产生的原信号中并不存在的无规则的额外 ...

  5. 华三IRF配置

    配置步骤: IRF的配置步骤: 1.配置IRF域(域ID=10.成员ID.优先级) irf domain 10irf member 1 priority 10irf member 2 priority ...

  6. 【luoguP5490】【模板】扫描线

    求\(n\)个矩形的面积并,可以用线段树维护一条垂直于\(y\)轴的直线上被矩形覆盖的长度有多少长,将直线从左往右扫一遍,遇到矩形左边界就+1,遇到右边界就-1,不为\(0\)的位置就表示没有覆盖 不 ...

  7. KD-Tree总结

    KD-Tree总结 问题引入 平面上有\(n\)个点,\(q\)组询问,每一次查询距离\((x,y)\)最近的点对,强制在线. 问题解决 暴力 显然我们可以直接枚举点然后算距离取\(min\),这样子 ...

  8. nginx之allow、deny

    allow和deny这两个指令的意思是指,允许ip和限制ip 在此之前不得不提一下,这两个指令是存在于ngx_http_access_module模块之中的 allow语法:allow address ...

  9. Out of range value for column 'field_length'

    往mysql数据库中插入数据是报错: Out of range value for column 'field_length' 字段类型是tinyint(4) 一开始以为是长度太小造成的,改成tiny ...

  10. 【转】干货篇:手机绕过BL锁9008模式强刷

    <ignore_js_op> 高通QPST线刷其实就是利用高通芯片自带的9008端口,将手机系统内的所有分区的镜像文件,直接刷写手机.这个刷机方式比REC卡刷和fastboot线刷,更底层 ...