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. Python 用户交互程序Input

    1.用户交互,用户输入 用户输出命令是input. 设置一个变量并输出. name = input("name:") print(name) 注:python2 下 raw_inp ...

  2. Linux-Centos7 安装图形界面

    1.首先安装X(X Window System),命令为 :yum groupinstall "X Window System" 回车(注意有引号) 2.查看桌面列表 : yum ...

  3. 性能测试监控工具nmon详解和分析

    性能测试监控工具nmon详解和分析 1.命令安装 1.查看liunx版本版本x86_64_14i 目录:cd /nmon/logs/ 版本x86_64_14i [root@localhost u06] ...

  4. 修改mongodb(带仲裁节点的副本集)各机器端口

    需求:因为端口调整,需要改变副本的备份集 1.查看当前的副本集信息 [root@localhost bin]# ./mongo 192.168.1.134:10001 repltest:PRIMARY ...

  5. Linux 安装搭建 tftpd 服务器

    ---------- For Ubantu 18.0.4 ---------- 0.安装tftp-server sudo apt-get install tftpd-hpa (服务器端) sudo a ...

  6. Paper Read: Convolutional Image Captioning

    Convolutional Image Captioning 2018-11-04 20:42:07 Paper: http://openaccess.thecvf.com/content_cvpr_ ...

  7. 命令行找不到genstrings问题tip

    问题:初次使用命令行genstrings,提示使用的是命令行工具而非xcode,无法直接使用genstrings. 解决方案:命令行输入sudo xcode-select --switch /Appl ...

  8. jQuery学习笔记(一)

    jQuery 事件 - ready() 方法 实例 在文档加载后激活函数(): $(document).ready(function(){ $(".btn1").click(fun ...

  9. GitHub的Windows客户端的使用教程

    GitHub for Windows客户端的使用教程 什么是Github >说到什么是GitHub,我们先看wikipedia的描述“GitHub是一个利用Git进行版本控制.专门用于存放软件代 ...

  10. Kotlin 条件控制

    IF 表达式 一个 if 语句包含一个布尔表达式和一条或多条语句. // 传统用法 var max = a if (a < b) max = b // 使用 else var max: Int ...