LeetCode – 453. Minimum Moves to Equal Array Elements

Given a non-empty an integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n – 1 element by 1.

Example:

Input:  [1,2,3]  Output: 3 Explanation:  Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]  
This problem is very interesting. Adding 1 to n-1 numbers is equivalent to subtracting 1 from a number. So, we find the smallest one, and then check how many times we need to subtract 1 from each of the numbers.

public class Solution { public int minMoves(int[] nums) { int min = Integer.MAX_VALUE; int step =0; for(int i=0;i 

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"LeetCode – 453. Minimum Moves to Equal Array Elements"

129
0 0 129

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top