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;
}
};
相关文章
- 03-13453. Minimum Moves to Equal Array Elements
- 03-13LeetCode 453 Minimum Moves to Equal Array Elements
- 03-13LeetCode #1608. Special Array With X Elements Greater Than or Equal X
- 03-131608. Special Array With X Elements Greater Than or Equal X
- 03-13[LeetCode] 462. Minimum Moves to Equal Array Elements II
- 03-13462. Minimum Moves to Equal Array Elements II
- 03-13【leetcode】1608. Special Array With X Elements Greater Than or Equal X
- 03-13【leetcode】1551. Minimum Operations to Make Array Equal
- 03-13[LeetCode] 1887. Reduction Operations to Make the Array Elements Equal
- 03-13Leetcode-462 Minimum Moves to Equal Array Elements II(最少移动次数使数组元素相等 II)