Leetcode 453 Solution
This article provides solution to leetcode question 453 (minimum-moves-to-equal-array-elements)
Access this page by simply typing in "lcs 453" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/minimum-moves-to-equal-array-elements
Solution
class Solution {
public:
int minMoves(vector<int>& nums) {
if (nums.size() == 0)
return 0;
int min_val = INT_MAX;
for (auto it = nums.begin(); it != nums.end(); it++)
min_val = min(min_val, *it);
int total = 0;
for (auto it = nums.begin(); it != nums.end(); it++)
total += *it - min_val;
return total;
}
};