当前位置:网站首页>453.最小操作数使数组元素相等
453.最小操作数使数组元素相等
2022-07-30 05:38:00 【Linke66】


关键:
让n-1个元素加1,就相当于选一个元素-1;
选n-1个元素加1,加到所有元素相等,就相当于选1个元素减1,减到所有的元素都等于最小元素。
class Solution {
public:
int minMoves(vector<int>& nums) {
//得到nums中的最小元素
int min_val=INT_MAX;
for(auto num:nums)
{
min_val=min(min_val,num);
}
//计算需要操作的次数
int ret=0;
for(auto num:nums)
{
ret+=num-min_val;
}
return ret;
}
};
边栏推荐
- Prime numbers (Tsinghua University computer test questions) (DAY 86)
- "Hou Lang" programmer version, a speech dedicated to a new generation of programmers, He Bing's "Hou Lang" speech imitation show
- 使用DataEase开源工具制作一个高质量的数据大屏
- MYSQL-InnoDB的线程模型
- [Other] DS5
- MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
- MySQL 灵魂 16 问,你能撑到第几问?
- Basic syntax of MySQL DDL and DML and DQL
- cnpm installation steps
- SOA(面向服务架构)是什么?
猜你喜欢
随机推荐
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
2022年比若依更香的开源项目
cmd(命令行)操作或连接mysql数据库,以及创建数据库与表
[Koltin Flow (1)] Five ways to create flow
np.argsort()函数详细解析
np.where()用法
SOA(面向服务架构)是什么?
“tensorflow.keras.preprocessing“ has no attribute “image_dataset_from_directory“
enumerate() 函数
MySQL-Explain详解
手把手教你设计一个CSDN系统
Mysql8.+学习笔记
MySQL(4)
微积分 / 自动求导
IDEA的database使用教程(使用mysql数据库)
【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
[Image processing] Image skeleton extraction based on central axis transformation with matlab code
MySQL database basics (a systematic introduction)
839. 模拟堆




![[Other] DS5](/img/20/6863bb7b58d2e60b35469ba32e5830.png)




