One pass JS solution


  • 0
    L
    function minMoves(nums) {
        let diff = 0;
        let min = Number.MAX_VALUE;
        
        nums.forEach((v, i) => {
            diff += v < min ? i * (min - v) : v - min;
            min = Math.min(v, min);
        });
        
        return diff;
    }
    

Log in to reply
 

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