Simple c++ solution


  • 0
    I

    Based on the idea that add 1 to n-1 number is same as subtract 1 from one number. And try to make all number equal to the minimum in the list.

    class Solution {
    public:
        int minMoves(vector<int>& nums) {
            if (nums.empty()) return 0;
            int min = nums[0];
            long sum = 0;
            for (auto& i : nums) {
                min = std::min(min, i);
                sum += i;
            }
            return sum - nums.size() * min;
        }
    };
    

Log in to reply
 

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