zoukankan      html  css  js  c++  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]
    
    纯数学题,面积法求解!!!

    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;
        }
    
  • 相关阅读:
    浅谈分布式 ID 的实践与应用
    Mobile DevOps 之 Proxmox 实现节流提效
    AI驱动的超分辨技术落地实践
    element ui 的 element-tree文字显示不全的问题
    Skype for business Server 2019 私有化部署
    VSCode+XAMAPP的PHP断点调试环境搭建XDebug
    Office的密码破和原理说明Excel,ppt,word
    Exchange2016 归档功能设置保留策略就地存档Archive
    基于windows server216自带的备份服务windows server backup
    C# 操作xml基础整理【转】
  • 原文地址:https://www.cnblogs.com/bonelee/p/8646707.html
Copyright © 2011-2022 走看看