LeetCode_453. Minimum Moves to Equal Array Elements
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 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]
package leetcode.easy;
public class MinimumMovesToEqualArrayElements {
public int minMoves(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int min = nums[0];
for (int i = 0; i < nums.length; i++) {
min = Math.min(min, nums[i]);
}
int moves = 0;
for (int i = 0; i < nums.length; i++) {
moves += nums[i] - min;
}
return moves;
}
@org.junit.Test
public void test() {
int[] nums = { 1, 2, 3 };
System.out.println(minMoves(nums));
}
}
LeetCode_453. Minimum Moves to Equal Array Elements的更多相关文章
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- [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 ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- Leetcode-462 Minimum Moves to Equal Array Elements II
#462. Minimum Moves to Equal Array Elements II Given a non-empty integer array, find the minimum n ...
- 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 ...
- LeetCode 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47
453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...
- [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 ...
- 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 ...
随机推荐
- 说说openjdk及G1回收器日志内容详细分析
谈谈openjdk: 在正式往下学习JVM之前,这里谈谈openjdk这个网站,这个在学习java并发时也用过它来分析过锁的底层实现,如:https://www.cnblogs.com/webor20 ...
- Spring动态切换多数据源事务开启后,动态数据源切换失效解决方案
关于某操作中开启事务后,动态切换数据源机制失效的问题,暂时想到一个取巧的方法,在Spring声明式事务配置中,可对不改变数据库数据的方法采用不支持事务的配置,如下: 对单纯查询数据的操作设置为不支持事 ...
- Insecure Code Management
Insecure Code Management-------------不安全的代码管理 Get the password (in clear text) from the admin accoun ...
- 11.04Test
11.04Test 查看请点个赞 转载请注明出处(~不然~) 题目 描述 做法 \(BSOJ5143\) 要求给\(M\)个通道染色,使得同色通道不能相交 转为矛盾模型,\(2-sat\)or二分图染 ...
- 压缩、解压命令——gzip、gunzip、tar、zip、bzip2
1.gzip命令与gunzip命令(压缩解压文件): (1)gzip——压缩: 利用touch命令创建一个文件,并向里面写入内容: 对创建的文件进行压缩: (2)解压缩gzip -d或gunzip: ...
- Reactive Extensions (Rx) 入门(3) —— Rx的事件编程
译文:https://blog.csdn.net/fangxing80/article/details/7628322 原文:http://www.atmarkit.co.jp/fdotnet/int ...
- A1138 | 根据前序、中序生成后序
参考了博客上码量不到50行的代码,完成了这题的AC重构.感觉真的基础很重要,这题其实是很简单的一道树的前中后序的题目.但是我之前练习的时候,都是用的自己总结的骚套路,虽然理解起来很直观,但是用了动态数 ...
- luoguP1742 最小圆覆盖
最小圆覆盖 首先 没错,我是个蒟蒻.luogu 流程 圆 C; for(i=1 to n) { if(P[i] 不在 C 内) { C = {P[i], 0}; for(j=1 to i-1) { i ...
- 【JZOJ6232】【20190625】喜欢最最痛
题目 \(n\)个节点的树,边权为正整数. 从1 号点开始走一个路径并最终回到 1 号点,且这条路径经过了所有的边. 一条路径的代价就是它经过的边的边权之和. 可以加若干条额外边,第 i 条加的额外边 ...
- orm-1
- ORM什么是? 类名 ---> 数据库表 对象 ---> 记录 对象.属性 ---> 字段 - ORM的优缺点: 优点: 可跨平台,可以通过对象.属性取值,对象.方法,让该方法内 ...