【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
- Difficulty: Easy
题目描述
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]
题目大意
数组长度是n,每次把n-1个数字加1,问需要多少次才能让所有的数字都相等。
解题方法
方法一:模拟过程
我用的直接的方法,每次把数组排序,然后把前n-1个元素++,再排序,知道首尾元素相等即可。
根据测试用例,这个方法应该对的,但这个方法时间超出。
public class Solution {
public int minMoves(int[] nums) {
int count=0;
Arrays.sort(nums);
while(nums[0] != nums[nums.length-1]){
for(int i=0; i<nums.length-1; i++){
nums[i]++;
}
Arrays.sort(nums);
count++;
}
return count;
}
}
方法二:求和-n*最小值
看了高票答案之后,才明白,把其中最小的n-1个元素都++ 相当于 把最大的元素–;
我们的目标是把所有的元素搞相等,也就是每次把最大的元素-1 直到所有元素都等于最小元素即可。
故总的运算次数等于 所有元素与最小元素 的差 的和: sum(array) - n * minimum
public class Solution {
public int minMoves(int[] nums) {
int count=0;
int min=nums[0];
for(int num : nums){
min=Math.min(min,num);
}
for(int num : nums){
count += num - min;
}
return count;
}
}
AC: 14 ms
python的代码如下:
class Solution:
def minMoves(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sum(nums) - len(nums) * min(nums)
C++代码如下:
class Solution {
public:
int minMoves(vector<int>& nums) {
int mn = INT_MAX;
long long s = 0;
for (int n : nums) {
if (n < mn) mn = n;
s += n;
}
return s - nums.size() * mn;
}
};
方法三:排序
受到上面的想法的启发,反思方法一,没必要每次循环都排下序,按照相减的策略,排序后,第一个元素就是最小元素,再求出其他元素与最小元素的差的和即可。
排序算法的时间复杂度是O(nlog(n))
public class Solution {
public int minMoves(int[] nums) {
int count=0;
Arrays.sort(nums);
for(int num : nums){
count += num - nums[0];
}
return count;
}
}
AC:53 ms
python代码如下:
class Solution:
def minMoves(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
res = 0
for num in nums:
res += num - nums[0]
return res
官网给的解答很好,很全面而且配了视频。好评。https://leetcode.com/articles/minimum-moves-to-equal-array-elements/
日期
2017 年 1 月 7 日
2018 年 11 月 14 日 —— 很严重的雾霾
2018 年 12 月 14 日 —— 12月过半,2019就要开始
【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)的更多相关文章
- 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 ...
- LeetCode 453. Minimum Moves to Equal Array Elements C#
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- 13. leetcode 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 arr ...
- LeetCode: 453 Minimum Moves to Equal Array Elements(easy)
题目: Given a non-empty integer array of size n, find the minimum number of moves required to make all ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 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&Python] Problem 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 arr ...
- 453. Minimum Moves to Equal Array Elements
Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...
- 453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1.示例:输入:[1,2,3]输出:3解释:只需要3次移动(注意每次移动会增加两个元素 ...
随机推荐
- 自动添加shell脚本头部信息
autocmd BufNewFile *.sh exec ":call AddTitleForShell()" function AddTitleForShell() call a ...
- Excel-在整个工作簿中查找/替换
13.在整个工作簿中查找/替换 调范围为:工作簿,默认是工作表:
- 为 Rainbond Ingress Controller 设置负载均衡
Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.rbd-gateway 组件,实际上是好雨科技团队开发的一种 Ingress Contro ...
- 【每天五分钟大数据-第一期】 伪分布式+Hadoopstreaming
说在前面 之前一段时间想着把 LeetCode 每个专题完结之后,就开始着手大数据和算法的内容. 想来想去,还是应该穿插着一起做起来. 毕竟,如果只写一类的话,如果遇到其他方面,一定会遗漏一些重要的点 ...
- Azure Key Vault(二)- 入门简介
一,引言 在介绍 Azure Key Vault 之前,先简单介绍一下 HSM(硬件安全模块). -------------------- 我是分割线 -------------------- 1,什 ...
- HashMap有几种遍历方法?推荐使用哪种?
本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...
- 启动spark-shell --master yarn的bug
报错如下 18/06/06 15:55:31 ERROR cluster.YarnClientSchedulerBackend: Yarn application has already exited ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- DP-Burst Balloons
leetcode312: https://leetcode.com/problems/burst-balloons/#/description Given n balloons, indexed fr ...
- oracle 当月日历的sql
select max(sun) sun, max(mon) mon, max(tue) tue, max(wed) wed, max(thu) thu, max(fri) fri, max(sat) ...