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:

  1. Input:
  2. [1,2,3]
  3.  
  4. Output:
  5. 3
  6.  
  7. Explanation:
  8. Only three moves are needed (remember each move increments two elements):
  9.  
  10. [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
  1. package leetcode.easy;
  2.  
  3. public class MinimumMovesToEqualArrayElements {
  4. public int minMoves(int[] nums) {
  5. if (nums == null || nums.length == 0) {
  6. return 0;
  7. }
  8. int min = nums[0];
  9. for (int i = 0; i < nums.length; i++) {
  10. min = Math.min(min, nums[i]);
  11. }
  12. int moves = 0;
  13. for (int i = 0; i < nums.length; i++) {
  14. moves += nums[i] - min;
  15. }
  16. return moves;
  17. }
  18.  
  19. @org.junit.Test
  20. public void test() {
  21. int[] nums = { 1, 2, 3 };
  22. System.out.println(minMoves(nums));
  23. }
  24. }

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. poj1502 MPI Maelstrom(单源最短路)

    题意:表面乍一看output是输出最小值,但仔细研究可以发现,这个最小值是从点1到所有点所花时间的最小值,其实是访问这些节点中的最大值,因为只有访问了最长时间的那个点才算访问了所有点.所以求最短路之后 ...

  2. org.apache.ibatis.exceptions.PersistenceException:

    org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.Ill ...

  3. Redis Cluster数据分片机制

    复制粘贴自: https://www.e-learn.cn/content/redis/2344485, 点击链接访问原文 仅供个人学习参考之用, 如有侵权, 请联系删除! 高级开发不得不懂的Redi ...

  4. class struct Equals

    { class clsA { private int _i; public int I { set { _i = value; } get { return _i; } } } struct strc ...

  5. 浏览器渲染详细过程:重绘、重排和 composite 只是冰山一角

    https://juejin.im/entry/590801780ce46300617c89b8 渲染 这张很经典的图许多人都看过,其中的概念大家应该都很熟悉,也就是这么几个步骤:js修改dom结构或 ...

  6. [Javascript] Private Variables with IIFEs

    An IIFE (immediately invoked function expression) is when a function is called immediately after it ...

  7. [转载] miller rabin

    本文转载自https://www.cnblogs.com/zsq259/p/11602175.html Miller-Rabin 事先声明,因为菜鸡Hastin知识水平有限就是菜,因此语言可能不是特别 ...

  8. linux 打印当前工作目录

    pwd

  9. vue的认识===下载

    VUE:不建议直接操作DOM Vue.js是前端三大新框架:Angular.js.React.js.Vue.js之一,Vue.js目前的使用和关注程度在三大框架中稍微 胜出,并且它的热度还在递增 Vu ...

  10. chart.xkcd 可绘制粗略,开通,手绘样式的图表库

    chart.xkcd 可以用来绘制手绘样式的图表,使用简单,样式也挺好看 简单使用 代码 index.html <!DOCTYPE html> <html lang="en ...