Given a non-empty 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 elements 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]
最小值与其他数的差值为n1,n2,.... x=n1+n2+......;
public class Solution {
public int minMoves(int[] nums) {//Arrays.sort(nums);
int min=Integer.MAX_VALUE,sum=0;
for(int e:nums){
sum+=e;
if(min>e)
min=e;
}
return (sum-min*nums.length);
}
}
本文介绍了一个算法问题:如何通过最少的步数使数组中的所有元素相等,每一步操作可以将除一个元素外的所有元素增加1。文章给出了一个具体的例子,并提供了一种高效的解决方案。

326

被折叠的 条评论
为什么被折叠?



