Java O(n) Simple solution


  • 0
    U

    The idea is that increase n-1 element by 1 equals to decrease 1 element by 1.

    public int minMoves(int[] nums) {
        int min = Integer.MAX_VALUE;
        
        for (int i : nums) {
            min = Math.min(min, i);
        }
        
        int res = 0;
        for (int i : nums) {
            res += i - min;
        }
        
        return res;
    }

Log in to reply
 

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