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]

Solution:

incrementing n-1 elements by one, means every time need to increment 1 to every number except the maximum. but it's hard to implement.

Increment to every nums except max, is as same as decrement 1 on one number until every number equals min number.

 public class Solution {
public int MinMoves(int[] nums) {
if(nums.Length==)
{
return ;
}
int n = nums.Length;
//Find min value in nums;
int min =nums[];
foreach(int num in nums)
{
min = Math.Min(min, num);
} //calculate the difference of every num from nums and min; ths sum should be the min Moves
//add 1 to n-1 is same as minus 1 from the max each time.
int moves = ;
foreach(int num in nums)
{
moves +=(num-min);
}
return moves;
}
}

LeetCode 453. Minimum Moves to Equal Array Elements 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. 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 ...

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

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

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

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

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

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

  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. BST&AVL&红黑树简单介绍

    (BST&AVL&红黑树简单介绍) 前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm ...

  2. hdu 1671 Phone List(字典树)

    知道bug的时候我眼泪掉下来... 我的第一道字典树,看了字典树的注意事项和实现方式,我写这道题的时候格外认真,就是奔着1A去的.结果这是几A来着? 第一遍写的时候提交MLA,我看了一下,是因为我释放 ...

  3. 设置Linux可以查看历史命令的执行时间

    大家都知道Linux平台上,可以通过history命令查看最近所执行过的命令,但history命令默认所显示的只有编号和命令的,只知道命令是最近所执行的,但不知具体执行的日期.时.分.秒,有时给寻找证 ...

  4. import,include

    1. #import导入头文件,即:导入头文件中的内容到当前类 2. #import ""导⼊自定义类,#import <>导入类库中的头文件. 3.功能类似C语言中的 ...

  5. 【CentOS如何最小化安装】

    近来发现越来越多的运维小伙伴们都有最小化安装系统的洁癖,因此,找老男孩来咨询,这个"洁癖"好习惯啊,必须支持,,因此发布本文和大家分享下. (1)系统安装类型选择及自定义额外包组 ...

  6. smbaclient

    在linux中通过smbaclient获取windows的共享文件 列出windows的共享目录 $ smbclient -L .xxx -U administrator%password 进入指定共 ...

  7. php中switch语句case后表达式写法记录一

    可作等级评价: $var = 95; switch(true){ case $var < 100; $level = 1; break; case $var < 95; $level = ...

  8. [转]奇异值分解(We Recommend a Singular Value Decomposition)

    原文作者:David Austin原文链接: http://www.ams.org/samplings/feature-column/fcarc-svd译者:richardsun(孙振龙) 在这篇文章 ...

  9. Mysql 5.6 解压版配置方案

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-co ...

  10. BitMap - leetcode [位运算]

    136. Single Number 因为A XOR A = 0,且XOR运算是可交换的,于是,对于实例{2,1,4,5,2,4,1}就会有这样的结果: (2^1^4^5^2^4^1) => ( ...