LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/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]
题解:
Think on the other way, increasing n - 1 elements == decreasing 1 element.
Decrease all num to min(nums).
Time Complexity: O(n). n = nums.length.
Space: O(1).
AC Java:
public class Solution {
public int minMoves(int[] nums) {
int min = Integer.MAX_VALUE;
for(int num : nums){
min = Math.min(min, num);
}
int moves = 0;
for(int num: nums){
moves += (num - min);
}
return moves;
}
}
Could be one pass.
Time Complexity: O(n).
Space: O(1).
AC Java:
class Solution {
public int minMoves(int[] nums) {
if(nums == null || nums.length < 2){
return 0;
}
int min = Integer.MAX_VALUE;
int sum = 0;
for(int num : nums){
min = Math.min(min, num);
sum += num;
}
return sum - min * nums.length;
}
}
跟上Minimum Moves to Equal Array Elements II.
LeetCode Minimum Moves to Equal Array Elements的更多相关文章
- [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 最少移动次数使数组元素相等
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) 47
453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- LeetCode_453. Minimum Moves to Equal Array Elements
453. Minimum Moves to Equal Array Elements Easy Given a non-empty integer array of size n, find the ...
- 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 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
随机推荐
- MapReduce类型与格式(输入与输出)
一.输入格式 (1)输入分片记录 ①JobClient通过指定的输入文件的格式来生成数据分片InputSplit: ②一个分片不是数据本身,而是可分片数据的引用: ③InputFormat接口负责生成 ...
- LATTICE 存储之FIFO的使用
坑,,以后填 对于Lattice 的 FIFO 存储器分为两种,见下图: 这两个的主要区别是一个后面加DC一个不加,那这个DC是什么意思呢??DC这里是Dual Clock的意思,也就是双时钟的意 ...
- 【Linux】Linux中常用操作命令
博客已转移,请借一步说话,http://www.weixuehao.com/archives/25 Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linu ...
- PAT A 1022. Digital Library (30)【结构体排序检索】
https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #incl ...
- 初学微信小程序
最近微信推出了微信小程序,为此我学了几天,基本了解了组件及简单语法,但是今天我自己想要独立写一个demo时,忽然发现难道我的不是微信小程序的语法(我以前是iOS 开发,不用css),而是css样式的设 ...
- 表单验证——jquery validate使用说明【另一个教程】
[参考:http://www.tuicool.com/articles/y6fyme] jQuery Validate jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证 ...
- MongoDB-C#驱动帮助
查增改删 链接字符串 MongoDB超管+(admin) 单独库用户不加 static string mongoR = string.Format("mongodb://{0}(admin) ...
- strftime 日期时间格式化
strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串. size_t strftime(char *strDest,size_t maxsiz ...
- Java web servlet 拦截器 以登陆为例子
以登陆为例子............... public class LoginFilter implements Filter { @Override public void destroy() { ...
- 团队作业week9 scenario testing
1.How do you expect different personas to use your software? What’s their need and their goals, how ...