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. LeetCode 531. Lonely Pixel I

    原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...

  2. 线程太多导致socket连接池爆满,进程启动不了

    Issue: 某部机上跟其它机器的连接有问题,ping可以通,telnet端口不通,可以其它机器可以连接到该机器上的进程. java应用启动不起来,产生以下错误. java.net.SocketExc ...

  3. 面试15--strcmp,strcpy,memmove实现

    一. strcmp strcmp是用于比较两个字符串的大小的.   int strcmp( const char *string1, const char *string2 ) char *strin ...

  4. 关于kmp算法

    字符串匹配算法简称kmp 日常安利大佬博客(真的是一篇很好的文章) 觉得百度百科讲的也挺好 就是给出两个字符串a, b 求b在a中的所有位置 next数组:代表当前字符之前的字符串中,有多大长度的相同 ...

  5. SpringCloud:搭建基于Gateway的微服务网关(二)

    0.代码 https://github.com/fengdaizang/OpenAPI 1.引入相关依赖 pom文件如下: <?xml version="1.0" encod ...

  6. 【CSP膜你赛】ATM

    题目描述 小沈阳在小品里说过:“人生最痛苦的事情是人死了,钱还没花掉”. 于是小宋(80 岁)决定要将所有的储蓄从 ATM 机中取出花光. 小宋忘记 了她有多少存款(银行卡密码她是记得的 2333), ...

  7. 微服务看门神-Zuul

    Zuul网关和基本应用场景 构建微服务时,常见的问题是为系统的客户端应用程序提供唯一的网关. 事实上,您的服务被拆分为小型微服务应用程序,这些应用程序应该对用户不可见,否则可能会导致大量的开发/维护工 ...

  8. xargs 命令教程

    转自阮一峰 http://www.ruanyifeng.com/blog/2019/08/xargs-tutorial.html 仅供个人交流学习 xargs是 Unix 系统的一个很有用的命令,但是 ...

  9. mariadb新安装解决远程访问以及root登录

    mariadb新安装解决远程访问以及root登录一.修改/etc/mysql/my.conf找到bind-address = 127.0.0.1这一行直接#掉或者改为bind-address = 0. ...

  10. Xor-matic Number of the Graph-CodeForces - 724G

    Xor-matic Number of the Graph-CodeForces - 724G 线性基棒题 建议做这题前先看看线性基的概念,然后A掉这道题--->路径最大异或和 这两个题都用到了 ...