当前位置:网站首页>Leetcode- minimum number of operations to make array elements equal - simple
Leetcode- minimum number of operations to make array elements equal - simple
2022-06-13 05:49:00 【AnWenRen】
title :453 The minimum number of operations makes the array elements equal - Simple
subject
Give you a length of
n
Array of integers for , Each operation will maken - 1
Elements added1
. Returns the minimum number of operations to make all elements of the array equal .
Example 1
Input :nums = [1,2,3]
Output :3
explain :
It only needs 3 operations ( Note that each operation increases the value of two elements ):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
Example 2
Input :nums = [1,1,1]
Output :0
Tips
n == nums.length
1 <= nums.length <= 105
-109 <= nums[i] <= 109
The answer is guaranteed 32-bit Integers
Code Java
public int minMoves1(int[] nums) {
// New knowledge Stream aggregate Array You can use Be similar to iterator Iterator
int min_value = Arrays.stream(nums).min().getAsInt();
int sum = 0;
for (int num : nums) {
sum += num - min_value;
}
return sum;
}
边栏推荐
- 2 first experience of drools
- 2021.9.29 learning log restful architecture
- Working principle of sentinel series (source code analysis)
- 为什么那么多人讨厌A-Spice
- MongoDB 多字段聚合Group by
- Difference between deviation and variance in deep learning
- 2021.9.29学习日志-MIME类型
- OpenGL Mosaic (8)
- 890. Find and Replace Pattern
- AUTOSAR actual combat tutorial pdf version
猜你喜欢
What happens when the MySQL union index ABC encounters a "comparison operator"?
13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
NVIDIA Jetson nano/xavier NX capacity expansion tutorial
Why do so many people hate a-spice
Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
OpenGL Mosaic (8)
Ffmpeg download suffix is Video files for m3u8
1 Introduction to drools rule engine (usage scenarios and advantages)
随机推荐
Feel the power of shardingsphere JDBC through the demo
Hump naming and underlining
Function and application scenario of field setaccessible() method
【自动化测试】Cypress手册
Agile conflicts and benefits
Interrupt processing
2021.9.29学习日志-MIME类型
Quartz database storage
Config server configuration center of Nacos series
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
How to view tongweb logs correctly?
2021.9.30 learning log -postman
Tongweb adapts to openrasp
Conf/tongweb Functions of properties
MySQL transactions and foreign keys
Tongweb crawl performance log script
How to Algorithm Evaluation Methods
Bicolor case
Leetcode- intersection of two arrays ii- simple
Use the browser to cut the entire page (take chrome as an example)