One Loop Simple C++ Solution


  • 0
    L
    class Solution {
    public:
        int minMoves(vector<int>& nums) {
            int moves = 0;
            int minVal = INT_MAX;
            for (int i = 0; i < nums.size(); i++) {
                if (minVal > nums[i]) {
                    moves += (minVal - nums[i]) * i;
                    minVal = nums[i];
                }
                else {
                    moves += nums[i] - minVal;
                }
            }
            return moves;
        }
    };

Log in to reply
 

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