作者: 负雪明烛
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就要开始

最新文章

  1. 3D Grid Effect – 使用 CSS3 制作网格动画效果
  2. Xcode8 pod install 报错 “Generating Pods project Abort trap
  3. iOS学习37数据处理之CoreData
  4. flatbuffers 使用问题记录
  5. 使用otl,报错:mysql Commands out of sync; you can&#39;t run this command now
  6. 如何解决Android的SDK与ADT不匹配问题
  7. BZOJ1049: [HAOI2006]数字序列
  8. python内置字符串操作方法
  9. 排序算法c语言描述---选择排序
  10. Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core
  11. SQL基础增删改查
  12. 12.Django思维导图
  13. 大数相加 Big Num
  14. Docker端口映射
  15. Exception while invoking TaskListener: Exception while invoking TaskListener: null
  16. 用jQuery实现简单的简单的轮播图
  17. POWERSHELL 计划任务的创建,收集DC中失败的登录信息并邮件通知
  18. VS2010中visual assist x的一些问题
  19. python 实现过滤出tomcat日志中含有ERROR 或Exception 的行并保存在另一个文件
  20. R语言 神经网络算法

热门文章

  1. Perl调用和管理外部文件中的变量(如软件和数据库配置文件)
  2. 自定义char类型字符,django中事务
  3. 进程和线程操作系统转载的Mark一下
  4. centos 7的命令变化
  5. Mybatis批量添加、更新小结
  6. 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明
  7. 容器中的容器——利用Dind实现开箱即用的K3s
  8. 学习java的第二十一天
  9. day20 系统优化
  10. 关于learning Spark中文版翻译