class Solution {
public int minMoves(int[] nums) {
Arrays.sort(nums);
int dp[] = new int[nums.length];
for(int i=1;i<nums.length;i++) {
dp[i] = nums[i] - nums[i-1] + dp[i-1];
}
int res = 0;
for(int i=nums.length -1; i>0; i--) {
res+=dp[i];
}
return res;
}
}
public int minMoves(int[] nums) {
int n = nums.length;
long min = nums[0], sum = 0;
for (int i : nums) {
min = Math.min(min, i);
sum += i;
}
return (int)(sum - min * n);
}
作者:AC_OIer
链接:https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-tt3zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论