leetcode 453. Minimum Moves to Equal Array Elements

本文探讨了如何找到使数组所有元素相等所需的最小移动次数。通过逆向思维和动态规划方法,文章提供了解决方案,即通过减小最大元素而非增加其他元素来达到目标。同时,给出了详细的算法实现和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

纯数学题,面积法求解!!!

class Solution(object):
    def minMoves(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # [1]=>0
        # [1,2]=>[2,2]=>1
        # [1,3]=>[2,3]=>[3,3]=>2
        # [1,2,3]=>[2,3,3]=>[3,4,3]=>[4,4,4]=>3
        # [1,2,4]=>[2,3,4]=>[3,4,4]=>[4,5,4]=>[5,5,5]=>4
        # ans = final_max-orig_min
        # greedy???dp???iter???  
        """
let’s define sum as the sum of all the numbers, before any moves; minNum as the min number int the list; n is the length of the list;

After, say m moves, we get all the numbers as x , and we will get the following equation

 sum + m * (n - 1) = x * n

and actually,

  x = minNum + m

and finally, we will get

  sum - minNum * n = m

So, it is clear and easy now.
        """
        return sum(nums) - min(nums) * len(nums)

另外一种思路就是:逆向思维!

Adding 1 to n - 1 elements is the same as subtracting 1 from one element, w.r.t goal of making the elements in the array equal.
So, best way to do this is make all the elements in the array equal to the min element.
sum(array) - n * minimum

 

 

最后就是DP,先看暴力解法,因为每次move最少的次数为max-min,?

 code like below:
    public int minMoves(int[] nums) {
        int res = 0;
        int n = nums.length;
        Arrays.sort(nums);

        while (nums[n - 1] != nums[0]) {
            int dis = nums[n - 1] - nums[0];
            for (int i = 0; i < n - 1; i++) {
                nums[i] += dis;
            }
            res += dis;

            //insert sort
            int max = nums[n - 1];
            int i = n - 2;
            while (i >= 0) {
                if (nums[i] > max) nums[i + 1] = nums[i--];
                else break;
            }
            nums[i + 1] = max;
        }

        return res;
    }

But it still Time Limit Exceeded.

============== The final solution is as follows ==============

The final flash, I though that should we use dynamic programming?

  • [step] is The number of steps arrive at the state of [all equal]
  • [finalNum] is The value of the state of [all equal]

we can know that

  • step[i] = (step[i-1] + num[i]) - finalNum[i-1] + step[i-1]
  • finalNum[i] = num[i] + step[i-1]
    public int minMoves(int[] nums) {
        Arrays.sort(nums);

        int n = nums.length;
        int step = 0;
        int finalNum = nums[0];

        for (int i = 1; i < n; i++) {
            int tmp = finalNum;
            finalNum = nums[i] + step;
            if (finalNum == tmp) continue;   //attention!!
            step = finalNum - tmp + step;
        }

        return step;
    }

转载于:https://www.cnblogs.com/bonelee/p/8646707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值