【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 ...
随机推荐
- PHP实现简单发红包(随机分配,平均分配)
最近碰到一些情况,把思路重新整理了一下,敲出代码.记下来,以后可以借鉴,进一步优化等. 大致的思路:红包主要分两种,一种是平均分配,一种是随机分配. 1.平均分配 平均分配相对好理解,只要把钱平均分给 ...
- day03变量的命名规范,常量,输出:自带换行,输入,注释,数据类型,运算符,常用字符大小关系
复习 ''' 1.语言的分类 -- 机器语言:直接编写0,1指令,直接能被硬件执行 -- 汇编语言:编写助记符(与指令的对应关系),找到对应的指令直接交给硬件执行 -- 高级语言:编写人能识别的字符, ...
- JavaScript形而上的策略模式
什么是策略模式? 先看代码片段1. // 代码片段1 var bonus = new Bonus(); bonus.setSalary(10000); bonus.setStrategy(new pe ...
- 一、Beego介绍与项目创建及启动
一.beego 简介 beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 torna ...
- [easyUI] 列表
一. 简述: 对一个层级的ul/ol进行调用menu()函数,即可简单做成层叠列表. 二. 实例: <ul id="menu3"> <li>Menu1 &l ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 条件设置行背景颜色
jQuery EasyUI 数据网格 - 条件设置行背景颜色 本教程将向您展示如何根据一些条件改变数据网格(datagrid)组件的行样式.当 listprice 值大于 50 时,我们将为该行设置不 ...
- three.js 创建点 线 面
<html> <head> <title>My first three.js app</title> <style> body { marg ...
- js中defer实现等文档加载完在执行脚本
我们可以使用defer来实现类似window.onload的功能: <script src="../CGI-bin/delscript.js" defer></s ...
- Mac+Apache+PHP 安装 Xdebug 方法
MAC homebrew自2018/3/31之后弃用homebrew/php By 31st March we will deprecate and archive the Homebrew/php ...
- Luffy之课程详情页
Luffy之课程详情页 提前写好课程详情的template,并放入到Vue中 注册路由 import CourseDetail from "../components/CourseDetai ...