453. Minimum Moves to Equal Array Elements
作者:互联网
class Solution {
public:
int minMoves(vector<int>& nums) {
// 题意:每次给n-1个值 +1, 最少多少次所有数字相等
// 等同于 每次给最大的值减1 ,最少多少次所有数字相等
int nTime = 0;
int nMinNum = nums[0];
for(int n = 1; n < nums.size(); n++)
{
if(nums[n] <= nMinNum)
{
nTime = nTime + n * abs(nMinNum-nums[n]);
nMinNum = nums[n];
}
else if(nums[n] > nMinNum)
{
nTime = nTime + (nums[n] - nMinNum);
}
}
return nTime;
}
};
标签:Elements,nums,int,453,Equal,最少,nMinNum,相等,nTime 来源: https://blog.csdn.net/dw903632958/article/details/101268500