453. Minimum Moves to Equal Arra
2018-04-11 本文已影响7人
安东可
453. Minimum Moves to Equal Array Elements
[思路]
-
给一个整数串,每次给n-1个元素加一,最后所有元素值相等;
-
数学问题:
sum + m* (n - 1) = x * n
变为
x = minNum + m
计算为:
sum - minNum * n = m
int minMoves(vector<int>& nums) {
int sum =0;
int min =0;
int len = nums.size();
if(len <=1)return 0;
min = nums[0];
for(int i=0;i<nums.size();i++)
{
sum += nums[i];
if(nums[i] < min)
min = nums[i];
}
return sum - min*len;
}