当前位置:网站首页>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;
}
}边栏推荐
- Force deduction solution summary 905- array sorted by parity
- 力扣解法汇总668-乘法表中第k小的数
- Graphic data analysis | business cognition and data exploration
- 力扣解法汇总450-删除二叉搜索树中的节点
- Spiral matrix (skill)
- 高考完不要急着去打工了,打工以后有的是机会,不差这三个月
- 广泛匹配修饰符符号已经被弃用,请勿使用
- The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
- Installing mysql-5.7 for Linux (centos7)
- 竞价广告每次点击出价多少钱是固定的吗?
猜你喜欢

The establishment and introduction of the announcement module of PHP development blog system

Alicloud OSS file upload system

阿里云oss文件上传系统

How to maximize the use of various matching methods—— Google SEM

How WPS inserts a directory and the operating steps for quickly inserting a directory

BaseDexClassLoader那些事

CVPR2022 | iFS-RCNN:一种增量小样本实例分割器

Installing MySQL version 5.5 database for Linux (centos6)

ozzanimation-基於sse的動作系統

MySQL高级部分知识点
随机推荐
leetcode:6. Zigzag transformation
PHP development 09 article module deletion and article classification writing
没有文笔,大家多多包涵
Don't miss it! Five large data visualization screens that HR must collect
Metaverse × How will smart cities develop?
Glfwpollevents() program crash
Smartbi helps you solve the problem of losing high-value customers
力扣解法汇总942-增减字符串匹配
Google 搜索广告系列设置前有哪些准备工作?
Installing MySQL version 5.5 database for Linux (centos6)
The most comprehensive redis transaction control in 2022 (with illustration)
How to locate keywords to make advertising accurate.
Oracle 11g graphic download installation tutorial (step by step)
xcall 集群脚本(查看jps命令)
Swiftyjson analyse les fichiers json locaux
力扣解法汇总965-单值二叉树
Graphic data analysis | business cognition and data exploration
通过搜索广告附加信息让广告更具相关性
Force deduction solution summary 396 rotation function
MySQL table common operation mind map