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的更多相关文章

  1. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  2. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Codeforces 438E The Child and Binary Tree - 生成函数 - 多项式

    题目传送门 传送点I 传送点II 传送点III 题目大意 每个点的权值$c\in {c_{1}, c_{2}, \cdots, c_{n}}$,问对于每个$1\leqslant s\leqslant ...

  2. 浅析vue实例的生命周期(生命周期钩子)

    “每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等” ,在不同的生命周期内会经历不同的钩子函数(生命周期 ...

  3. Learning-Python【9】:Python文件操作

    1.什么是文件 文件是操作系统提供给用户或应用程序的一种虚拟单位,该虚拟单位直接映射的是硬盘空间.通俗点说,文件就是存放数据的地方 2.为何要处理文件 用户或应用程序直接操作文件(读/写)就被操作系统 ...

  4. HADOOP HA 踩坑 - 所有 namenode 都是standby

    报错: 无明显报错 状况: 所有namenode都是standby,即ZK服务未生效 尝试一:手动强制转化某个namenode为active 操作:在某台namenode上,执行 hdfs haadm ...

  5. 揭示牌面使之升序 Reveal Cards In Increasing Order

    2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...

  6. webpack2与promise在IE环境下

    webpack2好像说是要自己编译es6,但是结果不是很理想,es6的箭头函数他就没有编译,所以目前还是先用babel来转换吧, 之前用的ajax是axios,底层是promise,但是promise ...

  7. python内置类型:列表,包括 list 和 tuple

    列表list 是一种有序的集合 ,假定list的名字为class list的元素个数:len( class) 访问元素: a. 索引从0开始    b. 也可以使用[-1],[-2],[-3] 从后面 ...

  8. JavaScript基础四

    1.13 Js中的面向对象 1.13.1 创建对象的几种常用方式 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 1.使用Object或 ...

  9. vue2.0s中eventBus实现兄弟组件通信

    在vue1.0中,组件之间的通信主要通过vm.$dispatch沿着父链向上传播和用vm.$broadcast向下广播来实现.然而在vue2.0中,已经废除了这种用法. vuex加入后,对组件之间的通 ...

  10. caffe安装

    安装caffe的时候一定要保持一个乐观的心态,不然容易放弃人生.由于自己是装完才写的,所以并没有截图. 平台:Window7 硬件:NVIDIV quaro M4000 软件:Visual Studi ...