Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array elements equal, where a move is incrementingn- 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个数字+1,那不等价于每次对1个数字-1么。给出答案
class Solution {
public int minMoves(int[] nums) {
int min = Integer.MAX_VALUE;
for(int num:nums) min = Math.min(num,min);
int result = 0;
for(int num:nums) result += (num-min);
return result;
}
}
 
 
 
 

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Activity的直接子类

    Activity的直接子类 AccountAuthenticatorActivity, AliasActivity ExpandableListActivity FragmentActivity Li ...

  2. 简单了解enum

    enum的性质: 1.枚举类型的实例都是常量 2.要使用enum,需要创建一个该类型的引用,并将其赋值给某个实例 3.常用的方法:  *     toString():某个enum实例的名字  *   ...

  3. vue搭建环境

    大早起的,没想自己起来那么早,既然起来了,就写点东西吧~最近在看Vue的东西,发现网上也是好多的资源,包括博客和视频 , 我是看的慕课网上的vue ,名字忘记了,价格148的,看了,也整理了笔记,看了 ...

  4. c专家编程摘录

    C专家编程摘录 c操作符的优先级 有时一些c操作符有时并不会像你想象的那样工作. 下方表格将说明这个问题: 优先级问题 表达式 期望的情况 实际情况 . 优先级高于* *p.f (*p).f *(p. ...

  5. Servlet与Jsp的结合使用实现信息管理系统二

    PS:前面说了抽取框架的搭建,接着就要我们开始进入网址的时候就要查到全部信息并显示在首页,我们用到的MySql数据库,具体步骤是: 创建数据库,创建表,添加信息 项目中调入mysql的jar包 mys ...

  6. java基础进阶一:String源码和String常量池

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/8046564.html 邮箱:moyi@moyib ...

  7. 【NOI2005】维护数列

    https://daniu.luogu.org/problem/show?pid=2042 一道伸展树维护数列的很悲伤的题目,共要维护两个标记和两个数列信息,为了维护MAX-SUM还要维护从左端开始的 ...

  8. C# Value type vs Reference type

    [MY NOTE]   [转载请注明出处] Reference Source: http://www.albahari.com/valuevsreftypes.aspx http://www.c-sh ...

  9. 1267 - Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=' | 1267 - Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (Latin,COERCIBL)

    select * FROM information_schema.columns WHERE table_schema = "databaseName" and collation ...

  10. POI颜色设置

    package com.java.connect.poi; import java.io.FileOutputStream; import java.io.IOException; import or ...