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 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#的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 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 ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- [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 ...
- 453. Minimum Moves to Equal Array Elements
Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...
- 453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1.示例:输入:[1,2,3]输出:3解释:只需要3次移动(注意每次移动会增加两个元素 ...
随机推荐
- WCF服务属性注入基础设施
WCF服务属性注入基础设施 WCF的服务的创建行为:使用默认构造函数创建WCF服务对象.如果我们想要在WCF内使用外部对象,最简单的方式就是把外部对象做成全局对象.然而这样的话会增加全局对象的数量,让 ...
- Windows下安装Redmine-2.5.3
安装准备 服务器操作系统:Windows Server 2008 R2 Standard,64位操作系统. RailsInstaller版本:2.2.4 (下载地址http://railsinsta ...
- 上传代码到GitHub时,遇到错误:fatal,The Requested URL return error 403
解决: from:pushing-to-git-returning-error-code-403-fatal-http-request-failed
- minSdkVersion与targetSdkVersion
targetSdkVersion是Android提供向前兼容的主要依据,在应用的targetSdkVersion没有更新之前,系统不会应用最新的行为变化 比如设置了app的targetSdkVersi ...
- find指令参数
1.name ~ 根目录 . 当前和子目录 name之后跟的是文件名 find . -name "[a-z]*[4-9].log" -print 2.perm perm后面跟的是权 ...
- Leetcode-33-Search in Rotated Sorted Array (Hard)
二分查找算法以及旋转之后的数组二分查找算法: #!/usr/local/bin/python3 # -*- coding: utf-8 -*- __author__ = 'author' class ...
- [JS]省市区数据及方法调用
调用方法: function GetProvinceByid(id) { if (id == null || id == undefined || id == "") return ...
- fileupload实现控制大小进行图片上传
if ($(".img-upload").length > 0) { $('.img-upload').fileupload({ type: 'POST', url: &qu ...
- schemes-universalLink-share_IOS-android-WeChat-chunleiDemo
schemes-universalLink-share_IOS-android-WeChat-chunleiDemo The mobile terminal share page start APP ...
- qwt的安装与使用
qwt简介 QWT,全称是Qt Widgets for Technical Applications,是一个基于LGPL版权协议的开源项目, 可生成各种统计图. 具体介绍,可参看官方网址:http:/ ...