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 ...
随机推荐
- web渗透—xss攻击如何防御
1.基于特征的防御 XSS漏洞和著名的SQL注入漏洞一样,都是利用了Web页面的编写不完善,所以每一个漏洞所利用和针对的弱点都不尽相同.这就给XSS漏洞防御带来了困难:不可能以单一特征来概括所有XSS ...
- Spring Boot 之:Spring Boot Admin
client 连接都 admin 时报错: 2019-08-22 11:58:37.695 ERROR 55095 --- [nio-8000-exec-1] o.a.catalina.connect ...
- python2和python3共存方法
拿到安装包,安装python3 centos: sudo yum install python36 ubuntu: sudo add-apt-repository ppa:deadsnakes/ppa ...
- python预课02 time模块,文本进度条示例,数字类型操作,字符串操作
time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...
- crontab每小时运行一次(转)
https://blog.csdn.net/liu0808/article/details/80668705 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错 ...
- windows强制删除文件和文件夹
包括只读类型 Deletes one or more files. DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] namesERASE [/P] [/F] [ ...
- 学习:STL_vector容器
vector基本概念: 功能: vector数据结构和数组非常相似,也称为单端数组 vector与普通数组区别: 不同之处在于数组是静态空间,而vector可以动态扩展 动态扩展: 并不是在原空间之后 ...
- starUML
下载地址: https://www.qqxiazai.com/down/10296.html 下载后解压,先运行 绿化.exe 然后右键管理员运行 StarUML.exe 进入后就可以画UML以及时序 ...
- 保存tensor至本地文件
path_temp = '../m39_data/temp3' #文件目录 file_name = os.path.join(path_temp, "setQ.txt") Q = ...
- 启动uiautomatorview 提示无法初始化主类
启动uiautomatorview 提示无法初始化主类, 重新安装jdk到1.8版本就好了,就是这么神奇.