zoukankan      html  css  js  c++  java
  • LeetCode: 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 (remember each move increments two elements):
    
    [1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]

    代码:

    这道题给了我们一个长度为n的数组,说是我们每次可以对n-1个数字同时加1,问最少需要多少次这样的操作才能让数组中所有的数字相等。那么我们想,为了快速的缩小差距,该选择哪些数字加1呢,不难看出每次需要给除了数组最大值的所有数字加1,这样能快速的到达平衡状态。但是这道题如果我们老老实实的每次找出最大值,然后给其他数字加1,再判断是否平衡,思路是正确,但是OJ不答应。

    正确的解法相当的巧妙,需要换一个角度来看问题,其实给n-1个数字加1,效果等同于给那个未被选中的数字减1,比如数组[1,2,3], 给除去最大值的其他数字加1,变为[2,3,3],我们全体减1,并不影响数字间相对差异,变为[1,2,2],这个结果其实就是原始数组的最大值3自减1,那么问题也可能转化为,将所有数字都减小到最小值,这样难度就大大降低了,我们只要先找到最小值,然后累加每个数跟最小值之间的差值即可,参见代码如下:

    1 class Solution {
    2 public:
    3     int minMoves(vector<int>& nums) {
    4         int mn = INT_MAX, res = 0;
    5         for (int num : nums) mn = min(mn, num);
    6         for (int num : nums) res += num - mn;
    7         return res;
    8     }
    9 };
  • 相关阅读:
    pzrNB!!!
    biu biu biu
    大笑三声再往下看!
    Selenium之Chrome浏览器的启动
    Selenium IDE脚本录制步骤简介
    Selenium IDE的一些操作
    Selenium IDE编辑区域修改操作学习
    在firefox安装Selenium IDE
    执行Java脚本firefox启动成功,不运行test方法,且提示NullPointerException
    Selenium+Python学习之一
  • 原文地址:https://www.cnblogs.com/llxblogs/p/7507535.html
Copyright © 2011-2022 走看看