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次移动(注意每次移动会增加两个元素 ...
随机推荐
- T_SQL查询语句(一): 单表查询
############################################ 查询语句--SELECT ########################################## ...
- Python之路3Day
3.python基础补充(集合,collection系列,深浅拷贝) 一.集合 1.集合(set): 把不同的元素组成一起形成集合,是python基本的数据类型.集合元素(set elements ...
- linux下php-5.4.8.tar.gz编译安装全攻略
首先安装基础依赖组建,注:这些依赖组建也是LINUX+PHP+MYSQL+APACHE+NGINX+MEMCACHED时必要的系统组件 LANG=C yum -y install gcc gcc-c ...
- 模拟Vue之数据驱动5
一.前言 在"模拟Vue之数据驱动4"中,我们实现了push.pop等数组变异方法. 但是,在随笔末尾我们提到,当pop.sort这些方法触发后,该怎么办呢?因为其实,它们并没有往 ...
- JavaScript系列文章:详解正则表达式之三
在上两篇文章中博主介绍了JavaScript中的正则常用方法和正则修饰符,今天准备聊一聊元字符和高级匹配的相关内容. 首先说说元字符,想必大家也都比较熟悉了,JS中的元字符有以下几种: / \ | . ...
- hibernate-部分字段查询方案
hibernate的延迟加载与本列记录不一样,延迟加载正常一般用于关联字段,或者大型字段使用. 本列的情况主要用于,某一张表有几十甚至上百个字段,例如财务报表等.但是在使用某些场景是却大多只是用其10 ...
- 构建jenkins
一.Jenkins简介: jenkins,之前叫做Hudson,是基于Java开发的一种持续集成工具,用户监控秩序重复的工作,包括: 1>持续的软件版本发布测试项目. 2>监控外部调 ...
- phpopp
<?php header("content-type:text/html;charset=utf8"); class lidepeng{ var $name; public ...
- [SQL基础教程] 4-3 数据的更新(UPDATE)
[SQL基础教程] C4 数据更新 4-3 数据的更新(UPDATE) UPDATE UPDATE <表名> SET <列名> = <表达式>; UPDATE &l ...
- 主成分分析(Principal components analysis)-最大方差解释
原文:http://www.cnblogs.com/jerrylead/archive/2011/04/18/2020209.html 在这一篇之前的内容是<Factor Analysis> ...