C# - one pass, adjust for min as you go


  • 0

    Same technique just an alternate to finding the min up front

        public int MinMoves(int[] nums) 
        {
            int min = nums[0];
            int moves = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] < min)
                {
                    moves += (min - nums[i]) * i;
                    min = nums[i];
                }
                else
                {
                    moves += nums[i] - min;
                }
            }
            
            return moves;
        }
    

Log in to reply
 

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