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;
        }
    
  • 相关阅读:
    C# CheckBoxList数据操作(转) 子曰
    extjs获取后台数据(asp.net) 子曰
    PHP学习系列之 环境配置
    Javah生成JNI头文件
    [转] ubuntu 终端命令
    我开博的这一年
    [原]Java web学习系列之 Java web开发中的Hibernate对象关系映射框架
    [原]Java web学习系列之开篇
    [原]android camera中的预览图片变形的解决办法
    [原]Java web学习系列之 Java web开发中数据库连接几种方法
  • 原文地址:https://www.cnblogs.com/bonelee/p/8646707.html
Copyright © 2011-2022 走看看