作者: 负雪明烛
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++)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  6. 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 ...

  7. [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 ...

  8. 453. Minimum Moves to Equal Array Elements

    Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...

  9. 453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等

    给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1.示例:输入:[1,2,3]输出:3解释:只需要3次移动(注意每次移动会增加两个元素 ...

随机推荐

  1. SNP 过滤(二)

    本文转载于https://www.jianshu.com/p/e6d5dd774c6e SNP位点过滤 SNP过滤有两种情况,一种是仅根据位点质量信息(测序深度,回帖质量等)对SNP进行粗过滤.如果使 ...

  2. Scrapy框架延迟请求之Splash的使用

    Splash是什么,用来做什么 Splash, 就是一个Javascript渲染服务.它是一个实现了HTTP API的轻量级浏览器,Splash是用Python实现的,同时使用Twisted和QT.T ...

  3. zabbix_get

    zabbix_get是一个命令行工具,可用于从一个远程的Zabbix探针获取监控数据. 1.2 用法 zabbix_get [-hV] [-s <host name or IP> ] [- ...

  4. Python os模块与sys模块

    1.os模块简单使用及说明 # -*- coding:utf-8 -*- """ os模块主要用于系统,处理程序与系统交互问题 大部分如属性等功能在linux系统中会使用 ...

  5. 使用dumi生成react组件库文档并发布到github pages

    周末两天玩了下号称西湖区东半球最牛逼的react文档站点生成工具dumi,顺带结合github pages生成了react-uni-comps文档站, 一套弄下来,感觉真香,现在还只是浅尝,高级的特性 ...

  6. Mybatis相关知识点(一)

    MyBatis入门 (一)介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code, ...

  7. HTTP 之 options预请求

    一.HTTP一共有八种常见请求方法 get:参数在url上,浏览器长度有限制,不安全 post:参数不可见,长度不受限制 put:上传最新内容到指定位置 delete:删除请求的url所表示的资源 h ...

  8. centos7安装Docker详细步骤(无坑版教程)

    一.安装前必读 在安装 Docker 之前,先说一下配置,我这里是Centos7 Linux 内核:官方建议 3.10 以上,3.8以上貌似也可. 注意:本文的命令使用的是 root 用户登录执行,不 ...

  9. Android 百度地图用法

    一.展示百度地图,并将一个指定的点(根据经纬度确定)展示在手机屏幕中心 1.下载百度地图移动版API(Android)开发包 要在Android应用中使用百度地图API,就要在工程中引入百度地图API ...

  10. 接口测试 python+PyCharm 环境搭建

    1.配置Python环境变量 a:我的电脑->属性->高级系统设置->环境变量->系统变量中的PATH变量. 变量名:PATH      修改变量值为:;C:\Python27 ...