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次移动(注意每次移动会增加两个元素 ...
随机推荐
- 在c#中运行js脚本(将js文件生成为.dll文件)
原文链接:http://www.cnblogs.com/xhan/archive/2010/10/22/1857992.html 前言: 本来在搞一个Google翻译的接口--向Google翻译发送请 ...
- 基础知识系列☞C#中→属性和字段的区别
"好吧...准备写个'基础知识系列',算是记录下吧,时时看看,更加加深记忆···" 其实本来准备叫"面试系列"... 字段.属性.你先知道的哪个概念? ***我 ...
- 在ashx中使用Server对象
Server.MapPath() System.Web.HttpContext.Current.Server.MapPath()
- 求方程式ax^2+bx+c=0的根。
#include <stdio.h>#include <stdlib.h>#include<math.h>int main(){ int a,b,c,d; doub ...
- 使用 IntelliJ IDEA 导入 Spark源码及编译 Spark 源代码
1. 准备工作 首先你的系统中需要安装了 JDK 1.6+,并且安装了 Scala.之后下载最新版的 IntelliJ IDEA 后,首先安装(第一次打开会推荐你安装)Scala 插件,相关方法就不多 ...
- MYSQL基础语句
参考书籍< MySQL数据库基础与实例教程> --孔祥盛 SQL(structured query language)结构化查询语言,应用最为广泛的关系型数据库语言. MYSQL属于关系型 ...
- 外国类似stackoverflow这样的网站访问慢怎么解决-遁地龙卷风
第二版 百度搜索蓝灯 下载桌面版 双击运行 如果打开的浏览器不是你想要的 拷贝地址栏地址给你想要的浏览器 一切就ok了!!!!! 建议不访问国外网站时,便将蓝灯关掉,否则在访问一些不开蓝灯能够正常访问 ...
- 百度定位API报错:leaked ServiceConnection com.baidu.location.LocationClient$1@426122f0
使用百度MapApi定位时候,当退出当时使用的activity后,则会报如题的异常,解决办法: 1:当退出当前定位的activity时,一定要在onDestroy方法中要mLocClient.stop ...
- python 列表推导式
squares = [x**2 for x in range(10)] 相当于squares = map(lambda x: x**2, range(10)),但是更简洁和易读.傻逼才会用最古老的fo ...
- Codeforces 710 D. Two Arithmetic Progressions
Description \(x=a_1k+b_1=a_2l+b_2,L\leqslant x \leqslant R\) 求满足这样条件的 \(x\) 的个数. Sol 扩展欧几里得+中国剩余定理. ...