【leetcode】453. Minimum Moves to Equal Array Elements
problem
453. Minimum Moves to Equal Array Elements
相当于把不等于最小值的数字都减到最小值所需要次数的累加和。
solution1:
class Solution {
public:
int minMoves(vector<int>& nums) {
int res = ;//err-initialization.
int mn = INT_MAX;//err.
for(auto num:nums) mn = min(num, mn);
for(auto num:nums) res += num-mn;
return res;
}
};
记得初始化,否则默认初始值是最小值。
solution2:
换一种思路,就是数组之和减去最小值乘以数组长度之积。
class Solution {
public:
int minMoves(vector<int>& nums) {
int mn = INT_MAX;
long sum = ;
for(int num:nums)
{
mn = min(num, mn);
sum += num;
}
return sum-mn*nums.size();
}
};
参考
1. Leetcode_453. Minimum Moves to Equal Array Elements;
2. GrandYang;
完
【leetcode】453. Minimum Moves to Equal Array Elements的更多相关文章
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- 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
Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...
- 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&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 ...
- 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 ...
随机推荐
- C# 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke
http://www.cnblogs.com/fish124423/archive/2012/10/16/2726543.html 在Invoke(....)之前加上1 this.components ...
- LNMP常用命令总结
1. 重启 ngnix: /usr/local/ngnix/sbin/nginx -s reload 2. 重启 php-fpm: 先查找php-fpm进程号 ps -aux | grep php-f ...
- 《温故而知新》JAVA基础五
定义:是类和类之间的关系"is a" 弗父类(基类)->子类(派生类) 是一直单继承的关系 好处:子类拥有父类的属性方法(private除外) 语法 class Son ex ...
- JS基础概念
JS基础概念 1. 算法及流程图 算法类型:1.算数算法:2.事务性算法(解决某个问题的方法和先后顺序). JS语法概述 1. 引入JS的方法 1.用<script src="&quo ...
- Node.js的缺陷
Node.js最大的优点是事件机制,一切皆在回调中触发(不阻塞).我想缺点或许有正在于此,方法没有返回值,而只能在回调中使用返回结果,会导致事件回调嵌套,代码结构很差. 在jQuery中有一套很好的机 ...
- pc远程控制凭证不工作的解决办法
感谢这位博主写的文章https://blog.csdn.net/u010433704/article/details/50679874 前提是要控制的电脑设置了密码,之前想不明白为什么需要这一项,后来 ...
- web项目执行流程
先扫描web.xml文件 jsp请求servlet servlet 调数据/不调数据 重定向/转发 Dao(封装数据) Biz(数据处理) 逻辑判段 返回前端界面显示
- Python—全局变量、局部变量、匿名函数
局部变量和全局变量 college1 = 'JMU' #全局变量 def change_name(name): college1 = 'LiGong' #局部变量,此函数是其作用域 # global ...
- 老司机浅谈linux系统学习技巧
Linux起源于20世纪70年代,是一种优秀的操作系统系统.初次接触到linux这个系统是在大学期间,这样才发现除了windows外的另外一个有趣系统.开始抱着好奇的心态去了解,随着深入学习,笔者被它 ...
- .net 在同步方法中使用拉姆达表达式执行async/await异步操作
代码如下: static void Main(string[] args) { ((Action)(async () =>{ var data = await HttpHelper.GetOnS ...