Another solution,do u understand how it works?


  • 0
    L
    class Solution {
    public:
        int minMoves(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            int l=nums.size();
            if(l==1) return 0;
            int moves=0;
            for(int i=2;i<=l;++i)
            {
              moves+=nums[i-1]-nums[i-2];
              if(i==l) return moves;
              nums[i]+=moves;
            }
        }
    };
    

    Do u understand how it works?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.