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*最小值 方法三: ...
随机推荐
- WebView基本使用
WebView mWebView; ProgressBar mProgressBar; mProgressBar = (ProgressBar) findViewById(R.id.news_prog ...
- [JAVA] BlockingQueue学习
有点时间,巩固巩固下基础知识:BlockingQueue,如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤 ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- 【MongoDB】4.MongoDB 原子修改器的 极速修改
文档转自:http://blog.csdn.net/mcpang/article/details/7752736 对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能够高 ...
- php 执行计划任务方式之 linux crontab 执行命令
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...
- java抽象类实践
package javaClassStudy; /** * * @author yuxg * 抽象类实践 */ public abstract class Person { private Strin ...
- 首师大附中互测题:99999999海岛帝国后传:算法大会【D001】
[D001]99999999海岛帝国后传:算法大会[难度:D] ———————————————————————————————————————————————————————————————————— ...
- JSON字符串语法
JSON 语法是 JavaScript 对象表示语法的子集. 数据在键/值对中展示, 多个数据由逗号分隔, 花括号保存一个对象, 方括号保存一个数组 JSON具有以下形式: 1. 对象(object) ...
- sphinx通过增量索引实现近实时更新
一.sphinx增量索引实现近实时更新设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少. 例如.原来的数据 ...
- hud 5876 2016 ACM/ICPC Asia Regional Dalian Online
题意:给一个图 给定一个点s 求补图中s点到达各个点的最短路 思路:从s点开始bfs 在图中与s点有连接的都是在补图中不能直接到达的点 反之在补图中都是可以直接到达的点 由此bfs ((( 诡异的写法 ...