/**
* sum is the sum of the array, min is the minimum of the array, f is the final value of the m moves
* m is # of moves, n is the size of the array
* sum + m(n-1) = f*n (1)
* min + m = f (2)
* we have m = sum - n*min
*/
public class Solution {
public int minMoves(int[] nums) {
int sum = 0, min = Integer.MAX_VALUE;
for (int num : nums) {
sum += num;
if (num < min) min = num;
}
return sum - nums.length*min;
}
}
Leetcode 453. Minimum Moves to Equal Array Elements
最新推荐文章于 2023-12-26 13:21:56 发布