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 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]
package leetcode.easy;

public class MinimumMovesToEqualArrayElements {
public int minMoves(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int min = nums[0];
for (int i = 0; i < nums.length; i++) {
min = Math.min(min, nums[i]);
}
int moves = 0;
for (int i = 0; i < nums.length; i++) {
moves += nums[i] - min;
}
return moves;
} @org.junit.Test
public void test() {
int[] nums = { 1, 2, 3 };
System.out.println(minMoves(nums));
}
}

LeetCode_453. Minimum Moves to Equal Array Elements的更多相关文章

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

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

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

  3. LeetCode Minimum Moves to Equal Array Elements II

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

  4. LeetCode Minimum Moves to Equal Array Elements

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

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

  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 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47

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

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

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

随机推荐

  1. ‘cmake' 不是内部或外部命令 也不是可运行的程序 或批处理文

    在 Win7下的命令行模式下,输入cmake相关命令,出现如下错误: ’cmake' 不是内部或外部命令 也不是可运行的程序 或批处理文件 解决方法: 在环境变量中添加cmake的文件路径. 计算机( ...

  2. git 学习笔记 —— 获取远端分支并修改后提交至远端仓库

    笔者最近进行开发过程中,所有参与者的代码需要通过 git 上传到远端仓库中,不同的模块对应不同的 git 分支,不同模块的数据需要从远端仓库中获取.这里记录下笔者从远端仓库中获取分支数据,进行修改,最 ...

  3. wordpress默认css样式class和id集合

    你是否想过如何设计WordPress主题的不同元素?每个主题都不一样,但是有一些CSS的class和id是由WordPress生成的.我们将逐一介绍一些最重要的默认WordPress样式,方便初学者快 ...

  4. 珠峰培训node 珠峰爬虫| cron 定时任务

    1.cron 定时任务 CronJob var CronJob = require('cron').CronJob; // 秒 分钟 时 天

  5. POJ3616-Milking Time-(dp)

    题意:牛有m个时间段可以挤奶,每个时间段的开始时间,结束时间,挤奶量不尽相同,寄完一次需要休息r时间,求在n时间内如何安排牛挤奶产量最大. 解题: 1.休息r时间,当做结束时间需要+r 2.以结束时间 ...

  6. Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)

    链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...

  7. 例程使用(1-4)共享内存 存图片+vector容器教程

    1传输的数据 1-1数据格式说明 1 两路视频图像Mat 图像 图像数据(Mat)+图像头信息(ImgInf) //图像的宽.高.类型信息 typedef struct { int width; // ...

  8. (2)在树莓派安装运行在Python3上的OpenCV

    https://www.jianshu.com/p/56929416b4a1 http://www.eeworld.com.cn/afdz/article_2018030511619.html htt ...

  9. MongoDB 启动报错

    1.配置MongoDB ls /etc/mongod.conf 可以根据此配置文件启动 或者根据自己需求进行配置文件的变更 重要提醒: 如果变更MongoDB配置文件中:日志与数据文件目录,那么要把这 ...

  10. Java验证jwt token

    https://jwt.io/ RS256加密JWT生成.验证 https://blog.csdn.net/u011411069/article/details/79966226 How to loa ...