[leetcode]453. Minimum Moves to Equal Array Elements
Analysis
pre做完一身轻松~—— [嘻嘻~]
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
这题要反过来思考,把其他数加一相当于把最大的数减一,所以只要计算其他数跟最小数的差值,然后相加就行了。具体证明及推导过程可以看这个博客:https://blog.csdn.net/u012814856/article/details/72710519
Implement
class Solution {
public:
int minMoves(vector<int>& nums) {
int res = 0;
sort(nums.begin(), nums.end());
for(auto num:nums){
res += (num-nums[0]);
}
return res;
}
};

159

被折叠的 条评论
为什么被折叠?



