当前位置:网站首页>Force deduction solution summary 462- minimum number of moves to make array elements equal II
Force deduction solution summary 462- minimum number of moves to make array elements equal II
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Give you a length of n Array of integers for nums , Returns the minimum number of moves required to make all array elements equal .
In one step , You can add... To an element in the array 1 Or minus 1 .
Example 1:
Input :nums = [1,2,3]
Output :2
explain :
Just two steps ( The instructions for each step add... To an element 1 Or minus 1):
[1,2,3] => [2,2,3] => [2,2,2]
Example 2:
Input :nums = [1,10,2,9]
Output :16
Tips :
n == nums.length
1 <= nums.length <= 105
-109 <= nums[i] <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-moves-to-equal-array-elements-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * In this question , The median is the reference value, which must be the minimum number of moves . * So first nums Sort , Find the difference between other numbers and the median .
Code :
public class Solution462 {
public int minMoves2(int[] nums) {
Arrays.sort(nums);
int middle = nums[nums.length / 2];
int count = 0;
for (int i : nums) {
count += Math.abs(middle - i);
}
return count;
}
}边栏推荐
- 力扣解法汇总462-最少移动次数使数组元素相等 II
- Force deduction solution summary 1728- cat and mouse II
- 力扣解法汇总417-太平洋大西洋水流问题
- PHP builds a high-performance API architecture based on sw-x framework (III)
- 消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
- How to locate keywords to make advertising accurate.
- 力扣解法汇总942-增减字符串匹配
- CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
- 力扣解法汇总905-按奇偶排序数组
- 2022西式面点师(技师)复训题库及在线模拟考试
猜你喜欢

Bracket generation (backtracking)

Introduction to SVM

Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started

Why do we use Google search ads?

matplotlib. pyplot. Bar chart (II)

Google Ads 竞价的运作机制

Ozzanmation action system based on SSE

Almost all schools will ask for the second round exam! Come in and recite the answer!

Operating mechanism of Google ads bidding

商城开发知识点
随机推荐
MySQL table common operation mind map
力扣解法汇总450-删除二叉搜索树中的节点
力扣解法汇总417-太平洋大西洋水流问题
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
Advantages of Google ads
力扣解法汇总436-寻找右区间
RPA introduction
力扣解法汇总883-三维形体投影面积
Spiral matrix (skill)
打包一个包含手表端应用的手机端APK应用—Ticwear
Ozzanation - système d'action basé sur sse
A mystery of the end of vagrant up
android html5页面加载缓存优化
Modification of system module information of PHP security development 12 blog system
力扣解法汇总473-火柴拼正方形
力扣解法汇总1728-猫和老鼠 II
Swiftyjson analyse les fichiers json locaux
What insurance products can you buy at the age of 60?
力扣解法汇总953-验证外星语词典
自适应搜索广告有哪些优势?