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次移动(注意每次移动会增加两个元素 ...
随机推荐
- 关于外部引用JS,中文乱码的问题
asp.net 页面默认编码为UTF-8, 如果js嵌套写在asp.net中,不会导致中文乱码,因为他们具有相同的编码 外部引用js由于编码格式与asp.net的编码不同,javascript编码默认 ...
- 微信连wifi正式全量对外开放申请 升级智能服务
之前我们提到过微信公众平台"微信连Wi-Fi"功能来了,昨日,微信连Wi-Fi自助申请入口正式全量对外开放(独立申请入口https://wifi.weixin.qq.com/),意 ...
- Linux 修改主机名 和 ip 映射关系
1. 修改主机名 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=hadoop 2. 修改主机名和IP的映射关系 vim /etc/hosts 1 ...
- 常用开源镜像站整理android sdk manager
http://www.cocoachina.com/programmer/20151023/13852.html http://android-mirror.bugly.qq.com:8080/inc ...
- bootstrap实现弹出窗口
bootstrap使用modal-dialog实现弹对话框. 一个对话框包含3部分: 对话框头部 modal-header 对话框内容体 modal-body 对话框底部 modal-footer 如 ...
- redis.1--SDS结构
1. Redis 没有直接使用c语言的字符串(以空字符结尾的字符数组),而是自己构建了一 种名为简单动态字符串(Simple Dynamic String , SDS),并将SDS做为 ...
- CSS相邻兄弟选择器
相邻兄弟选择器定义:选择紧接在另一个元素后的元素,而且两者有着相同的父元素. 代码一:<body> <h1>This is a heading.</h1> < ...
- 【转】phpcms-v9中关于模型的理解
PHPCMS v9 模型概念 一.什么是模型? 模型是系统知识的抽象表示.我们不能仅仅通过语言来描述一个系统,也不能仅仅通过记忆来记录关于系统的知识.知识是通过某种媒介来表达的,这种媒介所表达的内容就 ...
- BZOJ2171——K凹凸序列
好吧,我承认是sb题QAQ BZOJ2171弱化版QAQ 这题考试的时候写的我快吐血了QAQ 0.题目大意:给一个序列,你可以随便修改,修改是将一个数+1或-1,一次修改的代价是1,问把这个数修改成x ...
- 在framework中打包xib
废话不多说,直接上图 1.Copy Bundle Resources 中加入相关xib 2.这里是重点,调用的时候不能直接写 [[NSBundle mainBundle] loadNibNamed:@ ...