453. Minimum Moves to Equal Array Elements
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.
Example:
Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
每一步操作都给n-1个数同时+1, 问多少步操作后,使得数组里的每一个数都相当
把问题转换下 就是每一步操作都给1个数-1
C++(52ms):
1 class Solution { 2 public: 3 int minMoves(vector<int>& nums) { 4 int len = nums.size(); 5 if (len <= 1) 6 return 0 ; 7 int Min = nums[0] ; 8 int sum = 0 ; 9 for (int i = 0; i < len;i++ ){ 10 sum += nums[i] ; 11 if (nums[i] < Min) 12 Min = nums[i] ; 13 } 14 return sum-Min*len; 15 } 16 };