Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements E Qual, where a move is incrementing n -1 elements by 1.
Example:
Input:[1,2,3]output:3explanation:only three moves is needed (remember each move increments both elements): [+] =& gt; [2,3,3] = [3,4,3] = [4,4,4]
Think and "algorithm" knowledge unrelated to the simple problem is a bit difficult ah ... First fixed every time to make n-1 element +1, then this n-1 must be the smallest n-1, otherwise it will become more and more. Always find the smallest n-1 to add, until all the numbers are equal. To change the angle, it is equal to find the maximum number each time to make it-1, until the maximum and minimum number are equal. Eventually, all the numbers that are larger than the smallest are going to be the smallest numbers. So, the result is to ask for all the numbers and the minimum number of deviations. That is, the sum of all numbers and the difference between the minimum and the length of the array. The subject is a logical and mathematical simplification. The feeling algorithm sometimes is the mathematics competition ah ...
Java
class Solution { publicint minmoves (int[] nums) { int min = Nums[0], sum = 0; for (int num:nums) { if (num < min) = num; + = num ; } return sum-nums.length * min; }}
(Java) Leetcode 453. Minimum Moves to Equal array elements--the minimum number of moves makes the elements equal to the