当前位置:网站首页>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;
}
边栏推荐
- 890. Find and Replace Pattern
- Windbos run command set
- Bicolor case
- 使用cmake交叉編譯helloworld
- Leetcode- intersection of two arrays - simple
- How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
- MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
- SPI primary key generation strategy for shardingsphere JDBC
- @Detailed explanation of propertysource usage method and operation principle mechanism
- Django uses redis to store sessions starting from 0
猜你喜欢
Three paradigms of MySQL
Shardingsphere JDBC < bind table > avoid join Cartesian product
OpenGL马赛克(八)
The reason why the process cannot be shut down after a spark job is executed and the solution
软件测试——接口常见问题汇总
How to Algorithm Evaluation Methods
arrayList && linkedList
MongoDB 多字段聚合Group by
Sentinel series integrates Nacos and realizes dynamic flow control
How slow is the application system on tongweb? How dead is it?
随机推荐
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Some methods of string
Tongweb customs clearance guidelines
ArrayList loop removes the pit encountered
Service architecture diagram of Nacos series
Sentinel series hot spot current limiting
Pychart encountered time zone problem when connecting to MySQL timezone
OpenGL mosaic (VIII)
Pychart professional edition's solution to SQL script error reporting
2021.9.30 learning log -postman
Timeout thread log for tongweb
Ffmpeg download suffix is Video files for m3u8
Function and application scenario of field setaccessible() method
High availability of Nacos series
Basic application of sentinel series
Automatic database backup (using Navicat)
使用cmake交叉編譯helloworld
JNDI configuration for tongweb7
9. Errorstartevent and errorboundaryevent of error events
NVIDIA Jetson Nano/Xavier NX 扩容教程