当前位置:网站首页>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;
}
};
边栏推荐
- Seata exception: endpoint format should like ip:port
- 报错:npm ERR code EPERM
- mysql高阶语句(一)
- 2022年SQL经典面试题总结(带解析)
- 机器学习—梯度下降Gradient Descent Optimization—c语言实现
- [Koltin Flow (2)] The end operator of the Flow operator
- 数据操作 / 数据预处理
- navicat无法连接mysql超详细处理方法
- cnpm安装步骤
- Prime numbers (Tsinghua University computer test questions) (DAY 86)
猜你喜欢
随机推荐
Prime numbers (Tsinghua University computer test questions) (DAY 86)
期末作业C#实现学生宿舍管理系统
list(列表)和array(数组)的区别
号称年薪30万占比最多的专业,你知道是啥嘛?
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
解决phpstudy无法启动MySQL服务
MySql模糊查询大全
4461. Range Partition (Google Kickstart2022 Round C Problem B)
optimizer.zero_grad()
Graphic mirror symmetry (schematic diagram)
PyCharm usage tutorial (more detailed, picture + text)
Machine Learning - Gradient Descent Optimization - C language implementation
This dependency was not found:
Countdown (Source: Google Kickstart2020 Round C Problem A) (DAY 88)
MySQL(3)
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
create-nuxt-app创建出来的项目没有server
Seata exception: endpoint format should like ip:port
留念 · 大学时代最后的系统设计图
[GLib] What is GType