Skip to content

Latest commit

 

History

History
24 lines (24 loc) · 693 Bytes

453.md

File metadata and controls

24 lines (24 loc) · 693 Bytes

#453. Minimum Moves to Equal Array Elements 题目链接

public class Solution {
    /*
        一共有n个数字,每次有n-1个数字加一,
        可以转换为每次有一个数字减一,
        这样就可以转换为每个数见到最小数字的总和
     */
    public int minMoves(int[] nums) {
        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            if (num < min) {
                min = num;
            }
        }
        int moves = 0;
        for (int num : nums) {
            moves += (num - min);
        }
        return moves;
    }
}