原题链接在这里: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的更多相关文章

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

  2. LeetCode Minimum Moves to Equal Array Elements II

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...

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

  4. LeetCode 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47

    453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...

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

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

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

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

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

  9. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

随机推荐

  1. Loadrunner监控Linux系统资源

    一.安装rsh和rpcbind 1.查看是否安装:rpm -qa |grep rsh 2.安装rsh:yum -y install rsh* 3.yum  -y install nfs-utils r ...

  2. C++中随机数和不重复的随机数

    随机数 int rand (); rand函数返回一个0-32767之间的一个伪随机整数,32767可以由宏RAND_MAX表示,该函数需要头文件<stdlib.h>. 常用的是如何生成一 ...

  3. C# WebBrowser不能运行打开页面的activex

    如果是在64位操作系统中编译的代码,如果activex是32位的那么将不会运行32位的activex. 解决方法:右击项目->属性->生成->目标平台[x86]

  4. ExtJS 中自定义类

    首先我们来看一看在Javascript中,是怎样自定义类的: var Person = function (name, age) { this.Name = ""; this.Ag ...

  5. HYSBZ 2002 分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:中文题面 思路:考虑分块,每个位置维护一个跳出该块需要的步数cnt[],和跳出 ...

  6. mysql导入导出,及错误记录

    进入mysql的bin目录,如果mysql的bin添加了环境变量则不用. 导出,不指定编码则默认为:utf8mb4.: mysqldump -u root -h 127.0.0.1 -P 3307 - ...

  7. 超详细的Xcode代码格式化教程,可自定义样式

    为什么要格式化代码 当团队内有多人开发的时候,每个人写的代码格式都有自己的喜好,也可能会忙着写代码而忽略了格式的问题. 在之前,我们可能会写完代码后,再一点一点去调格式,很浪费时间. 有了ClangF ...

  8. 实现一个 能在O(1)时间复杂度 完成 Push、Pop、Min操作的 栈

    一,问题描述 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素.同时,栈的基本操作:入栈(Push).出栈(Pop),也是在O(1)时间内完成的. 二,问 ...

  9. WordPress 的 Google 字体问题解决办法

    在国内访问的时候,WordPress 里面引用的 google 字体可能会导致加载速度变得很慢. 要修改的地方有(我使用的版本是 4.0): wp-includes 里面的 script-loader ...

  10. JAVA_Android APK反编译就这么简单 详解(附图)

    在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用 ...