当前位置:网站首页>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;
}
};
边栏推荐
- Unity quickly builds urban scenes
- Why did Mr. Cao, a productionist in the field of high-end tea, choose Ruifeng L6 max?
- ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
- 【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
- PXE efficient batch network installation
- 【 Kotlin 中的类和对象实例】
- YOLOv3: An Incremental Improvement
- YOLOv3: An Incremental Improvement
- [NOIP2001 普及组]装箱问题
- 班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
猜你喜欢

"Xiao Deng's view" the value brought by Siem to enterprises (II)

Canvas -- draw text -- modification of pie chart

Intensive reading of the paper -yolov1:you only look once:unified, real time object detection

【创建交互式 Dice Roller 应用】

LoRa无线网关如何快速实现端到云的传输

Execution process behind shell commands

UDP和TCP可以使用同一个端口吗?

Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)

LeetCode·83双周赛·6128.最好的扑克手牌·模拟

Summary of Huawei virtualization fusioncompute knowledge points
随机推荐
LeetCode·
ELS initialization window class
2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
事半功倍:学会WEB性能测试用例设计模型
ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
tf.truncated_normal()用法
Hcip day 8 notes sorting (OSPF routing control, Appendix E, anti ring, shortest path calculation, republication))
LoRa无线网关如何快速实现端到云的传输
Cloud native guide what is cloud native infrastructure
Opencv error: (parameter or structure field)) unrecognized or unsupported array type in functon 'cvgetmat‘
Execution process behind shell commands
canvas——绘制图片——动图制作
els 注册窗口类、创建窗口类、显示窗口
[NOIP2001 普及组] 最大公约数和最小公倍数问题
Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting
[NOIP2001 普及组]装箱问题
Leetcode · daily question · 919. complete binary tree inserter · hierarchy traversal · BFS
ELS window settings, WM_ CREATE、WM_ PAINT
NFT因无意义而美丽
JSD-2204-酷鲨商城(管理商品模块)-Day02