• 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;
        }
    
  • 相关阅读:
    Maven命令行使用:mvn clean package(打包)
    快速构建 Spring Boot 应用
    Spring MVC 表单处理
    Spring Web Hello World 例子
    Spring 事务管理
    开发环境搭建
    考研计算机专业课练习题
    考研计算机自测练习
    考研计算机自测练习答案
    考研计算机专业常见术语
  • 原文地址:https://www.cnblogs.com/bonelee/p/8646707.html
Copyright © 2020-2023  润新知