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*最小值 方法三: ...
随机推荐
- mysql缓存
<cache/>字面上看就是这样.这个简单语句的效果如下: 映射语句文件中的所有 select 语句将会被缓存. 映射语句文件中的所有 insert, update 和 delete ...
- Python常见问题及资料收集
1,字符编码处理: http://bbs.chinaunix.net/thread-1431029-1-1.html
- cryptkeeper的使用
转自:http://www.linuxidc.com/Linux/2011-06/37070.htm Cryptkeeper 是加密文件系统 EncFS 的前端工具,以 Gnome Applet 方式 ...
- FTP安装与使用
1.查看ftp是否已安装:rpm -qa | grep vsftpd 2.安装ftp:rpm -ivh vsftpd 或yum install vsftpd 3.匿名用户配置文件主要参数: anony ...
- 新的篇章--Python
这周已经开始Python的学习了,感觉Python类似于Powershell, 但又有不同点.在此总结一下新学到的资料: 简单的使用变量的方法: name= input("input you ...
- 从零开始山寨Caffe·叁:全局线程管理器
你需要一个管家,随手召唤的那种,想吃啥就吃啥. ——设计一个全局线程管理器 一个机器学习系统,需要管理一些公共的配置信息,如何存储这些配置信息,是一个难题. 设计模式 MVC框架 在传统的MVC编程框 ...
- Mysql创建新用户方法
1. CREATE USER 语法: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'dog'@'lo ...
- Odoo Email Template Problem
Odoo 8.0 的邮件模板是运行自jiajin2沙盒中的阉割版mako,像自定义及 <%%>等功能都无法正常使用. 且for-loop %for %endfor不能嵌套在table中使用 ...
- xampp安装
软件下载在以下网站 http://www.apachefriends.org/zh_cn/index.html XAMPP 是一个易于安装且包含 MySQL.PHP 和 Perl 的 Apache 发 ...
- HDU1242 BFS+优先队列
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...