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 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]
Summary:
对于一个长度为n的整型数数组,每步将n-1个元素加1,求最少需要多少步,能使数组中的数字全部相同。
Analysis:
若使数组尽快满足题目要求,则需要每次给除去最大值的其余数字加1,但这种方法效率过低,可以换一种思路。每次将数组中的n-1个数字加1,相当于将剩余的一个数字减1。所以只需找到数组中的最小值m,计算m与数组中其他数字差的累计和即可。
class Solution {
public:
int minMoves(vector<int>& nums) {
sort(nums.begin(), nums.end());
int len = nums.size(), res = ;
for (int i = ; i < len; i++) {
res += nums[i] - nums[];
}
return res;
}
};
LeetCode 453 Minimum Moves to Equal Array Elements的更多相关文章
- 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 ...
- 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次移动(注意每次移动会增加两个元素 ...
随机推荐
- MongoDB MapReduce学习笔记
http://cnodejs.org/topic/51a8a9ed555d34c67831fb8b http://garyli.iteye.com/blog/2079158 MapReduce应该算是 ...
- HDOJ 3652 B-number
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- oracle数据库表空间扩容方法
1. 先查询表空间在物理磁盘上存放的位置,注意使用sysdba的账号登陆. SELECT tablespace_name, file_id, file_name, ), ) total_space F ...
- 【PHP面向对象(OOP)编程入门教程】3.什么是面向对象编程呢?
就不说他的概念,如果你想建立一个电脑教室,首先要有一个房间, 房间里面要有N台电脑,有N个桌子, N个椅子, 白板, 投影机等等,这些是什么,刚才咱们说了, 这就是对象,能看到的一个个的实体,可以说这 ...
- Linux/CentOS配置:使用yum update更新时不升级内核的方法
RedHat/CentOS/Fedora使用 yum update 更新时,默认会升级内核.但有些服务器硬件(特别是组装的机器)在升级内核后,新的内核可能会认不出某些硬件,要重新安装驱动,很麻烦.所以 ...
- 基于Windows10安装Ubuntu双系统
步骤: 1.从Ubuntu的官网上下载Ubuntu的iSO安装包. http://www.ubuntu.com/download/ 我安装的版本是Ubuntu 14.04.3 LTS 64位版本 2. ...
- I/O复用机制概述
导读 /O多路复用技术通过把多个I/O的阻塞复用到同一个select的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求.与传统的多线程/多进程模型比,I/O多路复用的最大优势是系统开销小 ...
- iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html
iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html 前言 這裡只是講一個故事,一個發生在我身上的真實的故事.曾經,我以為搞加 ...
- Unable to locate player settings. bin/Data/settings.xml
Hello guys, so according to the this response: http://stackoverflow.com/a/18302624/5727136 you need ...
- mysql 时间戳 按周、日、月 统计方法 附 date格式
create_time时间戳格式 SELECT FROM_UNIXTIME(create_time,'%Y%u') weeks,COUNT(id) COUNT FROM role GROUP BY w ...