样例 1 输入: [3, 4, 6, 6, 3] 输出: 7 说明: [3, 4, 6, 6, 3] -> [4, 5, 7, 6, 4] -> [5, 6, 7, 7, 5] -> [6, 7, 8, 7, 6] -> [7, 8, 8, 8, 7] -> [8, 9, 9, 8, 8] -> [9, 9, 10, 9, 9] -> [10, 10, 10, 10, 10]
来源:https://tianchi.aliyun.com/oj/164426199705086870/193936950952137404
class Solution {
public:
/**
* @param arr: the array
* @return: determine the number of moves to make all elements equals
*/
long long arrayGame(vector<int> &arr) {
// write your code here
int ans = 0, m = *min_element(arr.begin(), arr.end());
for(int i = 0; i < arr.size(); ++i)
ans += arr[i]-m;
return ans;
}
};
50ms C++