C# - diff between each number and min to get total steps


  • 0
    public int MinMoves(int[] nums) 
    {
        // decreasing all but 1 element by 1 is same as increasing 1 element
        int min = nums[0];
        foreach (int num in nums)
        {
            min = num < min ? num : min;
        }
        int steps = 0;
        foreach (int num in nums)
        {
            steps += num - min;
        }
        return steps;
    }

Log in to reply
 

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