(Java) LeetCode 453. Minimum Moves to Equal Array Elements —— 最小移动次数使数组元素相等

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

Example:

Input:
[1,2,3]

Output:
3

Explanation:
Only three moves are needed (remember each move increments two elements):

[1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]


觉得和“算法”知识无关的简单题反而有点难度啊……首先固定了每次都要使得n-1个元素+1,那么这n-1个一定是最小的n-1个,否则就会越加越多。每次都找最小的n-1个去加,直到所有的数相等。换个角度,就等于每次找到最大的那个数使得其-1,直到最大和最小的数相等。最终,所有比最小的那个数大的数,都要变成最小的数。所以,结果就是求所有数与最小数差的和。即,所有数的和与最小数与数组长度乘积的差。本题就是个逻辑推理和数学式的简化。感觉算法有的时候就是数学竞赛啊……



Java

class Solution {
    public int minMoves(int[] nums) {
        int min = nums[0], sum = 0;
        for (int num : nums) {
            if (num < min)
                min = num;
            sum += num;
        }
        return sum - nums.length * min;
    }
}

原文地址:https://www.cnblogs.com/tengdai/p/9439615.html

时间: 2024-10-07 18:49:24

(Java) LeetCode 453. Minimum Moves to Equal Array Elements —— 最小移动次数使数组元素相等的相关文章

[LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

LeetCode 453. Minimum Moves to Equal Array Elements C#

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

LeetCode 462. Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

453. Minimum Moves to Equal Array Elements(LeetCode)

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

[LeetCode&amp;Python] Problem 453. Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

453. Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

453. Minimum Moves to Equal Array Elements (Easy)

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

【leetcode 简单】 第一百零三题 最小移动次数使数组元素相等

给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [1,2,3] 输出: 3 解释: 只需要3次移动(注意每次移动会增加两个元素的值): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] class Solution: def minMoves(self, nums): """ :type nums: List[int] :rtype: in

[Math_Medium]462. Minimum Moves to Equal Array Elements II

原题462. Minimum Moves to Equal Array Elements II 题目大意: 给你一串数字,对于每个数字,一次性可以移动一步(数值增加1或者减小1),请问如何在最小的步伐数内使所有的数字都相等 Example Input: [1,2,3] Output: 2 Explanation: Only two moves are needed (remember each move increments or decrements one element): [1,2,3]