当前位置:网站首页>leetcode-462.最少移动次数使数组元素相等
leetcode-462.最少移动次数使数组元素相等
2022-07-26 03:14:00 【KGundam】
数学问题
题目详情
给你一个长度为 n 的整数数组 nums ,返回使所有数组元素相等需要的最少移动数。
在一步操作中,你可以使数组中的一个元素加 1 或者减 1
示例1:
输入:nums = [1,2,3]
输出:2
解释:
只需要两步操作(每步操作指南使一个元素加 1 或减 1):
[1,2,3] => [2,2,3] => [2,2,2]
示例2:
输入:nums = [1,10,2,9]
输出:16
思路:
本题难思考的点是如何知道以哪个数为基准数来使其他数都来靠近他,从而操作最少,结论就是排序后的中位数
不妨简单证明一下:
假如有一个排序好的数组例如2 3 4 5 6 7 8 假如要寻找一个小于2或者大于8的基准数来操作,那么肯定不如选择数组范围内的数来做基准数,自己随便试验试验就可以知道了,2 3 4 5 6 7 8要选2~8
3 4 5 6 7就要选3~7
4 5 6就要选4~6
…
由此我们可以看出选中位数是最好的,所以本题即可转换为
寻找中位数的问题
我们可以先排序数组,然后直接获得中位数,代码如下:
我的代码:
class Solution
{
public:
int minMoves2(vector<int>& nums)
{
sort(nums.begin(), nums.end()); // 排序
// 中位数
int n = nums.size(), mid = nums[n / 2], res = 0;
for (int num : nums)
{
res += abs(num - mid); //注意取绝对值
}
return res;
}
};
在思路中我们通过基准数可以想到:快速排序!里面同样有基准数这一利用,所以我们同样可以使用快速排序寻找第n / 2大的数(假设数组长n),有关快速排序的方法可以看leetcode-215.数组中的第K个最大元素:
leetcode-215.数组中的第K个最大元素 题解
这里我就直接使用C++已经有的库函数了,代码如下:
class Solution
{
public:
int minMoves2(vector<int>& nums)
{
int n = nums.size(), mid = n / 2, res = 0;
// 第mid大的数字归位,nums[mid]即为中位数
nth_element(nums.begin(), nums.begin() + mid, nums.end());
for (int num : nums)
{
res += abs(num - nums[mid]);
}
return res;
}
};
边栏推荐
- URDF 语法详解
- 2022-07-21 第四小组 修身课 学习笔记(every day)
- How to close the case prompt icon of win11? Closing method of win11 case prompt Icon
- Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
- Get twice the result with half the effort: learn the web performance test case design model
- Docker installs redis!!! (including detailed illustration of each step) actual combat
- NFT因无意义而美丽
- Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
- els 修改光标、修改图标
- QT笔记——Q_Q 和Q_D 学习
猜你喜欢

ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比

Opening method of win11 microphone permission

js中数组排序的方法有哪些

Execution process behind shell commands

ES6 set and map

YOLOv3: An Incremental Improvement

LeetCode·

【 Kotlin 中的类和对象实例】

Alibaba Sentinel - 集群流量控制

Implement a method to find the sum of the number k and m in the array
随机推荐
Dominate the salary list! What industry has a "money" path?
Opening method of win11 microphone permission
Managing databases in a hybrid cloud: eight key considerations
阿里二面:千万级数据量的表,快速查询如何进行?
2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS
Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘
Canvas -- drawing of rectangle -- making of histogram
canvas——绘制文本——饼图的修改
There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first
els 回调函数、退出消息
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
How to close the case prompt icon of win11? Closing method of win11 case prompt Icon
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
离线数据仓库从0到1-阶段一资源购买配置
Is the galaxy VIP account opened by qiniu safe?
els 修改光标、修改图标
What are you interviewing for in a big factory? It's worth watching (I)
Functions and usage of snownlp Library