作者: 负雪明烛
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. R语言矩阵相关性计算及其可视化?

    目录 1. 矩阵相关性计算方法 base::cor/cor.test psych::corr.test Hmisc::rcorr 其他工具 2. 相关性矩阵转化为两两相关 3. 可视化 corrplo ...

  2. NextPolish对基因组进行polish

    NextPolish由未来组开发对基因组序列进行polish的工具,对三代以及二代均可进行polish. gituhp地址:https://github.com/Nextomics/NextPolis ...

  3. sprint-boot 日志

    市面上的日志框架: JUL.JCL.Jboss-logging.logback.log4j.log4j2.slf4j.... SpringBoot:底层是Spring框架,Spring框架默认是用JC ...

  4. Python序列化,json&pickle&shelve模块

    1. 序列化说明 序列化可将非字符串的数据类型的数据进行存档,如字典.列表甚至是函数等等 反序列化,将通过序列化保存的文件内容反序列化即可得到数据原本的样子,可直接使用 2. Python中常用的序列 ...

  5. Bootstrap实战 - 瀑布流布局

    讲 Bootstrap 基础的教程网上已经很多了,实际上 Bootstrap 中文网(bootcss.com)里的文档已经写的很详细了,但实战的案例却不多.这里用一些当前流行的网页布局为导向,使用 B ...

  6. C语言中的指针与整数相加的值计算

    以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数  3. 指针强制转换为另一个类型后(指针或者是整数)  +  整数 测试例子: 1 struct AAA{ int a; char b[ ...

  7. Typora数学公式输入指导手册

    Markdown 公式指导手册 公式大全的链接 https://www.zybuluo.com/codeep/note/163962#mjx-eqn-eqsample 目录 Markdown 公式指导 ...

  8. 使用Redis实现令牌桶算法

    在限流算法中有一种令牌桶算法,该算法可以应对短暂的突发流量,这对于现实环境中流量不怎么均匀的情况特别有用,不会频繁的触发限流,对调用方比较友好. 例如,当前限制10qps,大多数情况下不会超过此数量, ...

  9. shell awk命令字符串拼接

    本节内容:awk命令实现字符串的拼接 输入文件的内容: TMALL_INVENTORY_30_GROUP my163149.cm6 3506 5683506 mysql-bin.000013 3273 ...

  10. Project Reactor工厂方法和错误处理

    工厂方法创建流 Backpressure : the ability for the consumer to signal the producer that the rate of emission ...