[抄题]:

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]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

逆向思维:n - 1 个数+1是抬高标准,也可以降低标准 一个数-1

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

逆向思维的关键:抬高标准的效果=降低标准

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

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  n-1元素+1=一个元素-1

[关键模板化代码]:

[其他解法]:

[Follow Up]:

462. Minimum Moves to Equal Array Elements II 还是数学题

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int minMoves(int[] nums) {
//ini:sort
Arrays.sort(nums);
int res = 0, min = nums[0]; //find min
for (int num : nums) {
min = Math.min(min, num);
} //add res
for (int num : nums) {
res += (num - min);
} //return
return res;
}
}

453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的的更多相关文章

  1. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  2. LeetCode 453 Minimum Moves to Equal Array Elements

    Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...

  3. LeetCode 453. Minimum Moves to Equal Array Elements C#

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  4. 13. 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 arr ...

  5. 453. Minimum Moves to Equal Array Elements

    Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...

  6. [LeetCode&Python] Problem 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 arr ...

  7. 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 ...

  8. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  9. 453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等

    给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1.示例:输入:[1,2,3]输出:3解释:只需要3次移动(注意每次移动会增加两个元素 ...

随机推荐

  1. iOS UI调试工具 -- UIDebuggingInformationOverlay

    英文原文: http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/ 无意中看到iOS自带调试工具 ...

  2. gulp 合格插件评判标准

    官方插件列表:    https://gulpjs.com/plugins/   合格插件的判断标准 1. 不修改内容 如果一个插件一个文件都修改(无论是文案内容,文件路径),那么它就不是一个gulp ...

  3. 调用 Windows 7 中英文混合朗读

    1.windows7内置语音识别 2.文本到语音转换增加了 Microsoft Lili - Chinese(China) ,支持中英文混合朗读 3.女声.部分词组基本发音准确 以上让我有做个小程序的 ...

  4. lnmp php7 安装mysqli扩展 undefined function mysqli_connect()

    在用ci框架的时候, https://blog.csdn.net/zqtsx/article/details/8746497 https://blog.csdn.net/move_now/articl ...

  5. Jenkins的用户管理

    用户管理入口 Jenkins首页有一个用户,但是只能从那查看用户列表和信息,管理用户的入口在Jenkins->系统管理->管理用户 新建用户 在管理用户左侧有一个新增用户,点击后按照表单填 ...

  6. FPGA中RAM使用探索

    FPGA中RAM的使用探索.以4bitX4为例,数据位宽为4为,深度为4. 第一种方式,直接调用4bitX4的RAM.编写控制逻辑对齐进行读写. quartus ii 下的编译,资源消耗情况. 85C ...

  7. TCP报文送达确认ACK

    TCP数据包中的序列号(Sequence Number)不是以报文段来进行编号的,而是将连接生存周期内传输的所有数据当作一个字节流,序列号就是整个字节流中每个字节的编号.一个TCP数据包中包含多个字节 ...

  8. PL/SQL 训练02--集合数组

    1. 请列举关联数组.嵌套表.VARRAY三种集合类型的区别区别:1 关联数组只能在plsql中使用,嵌套表,varray可用于sql中,数据库表中的列2 嵌套表,varray必须在使用的时候初始化, ...

  9. js中如何将字符串转化为时间,并计算时间差

    在前台页面开发时通常会用到计算两个时间的时间差,先在此附上实现方法 //结束时间 end_str = ("2014-01-01 10:15:00").replace(/-/g,&q ...

  10. 关系数据库域关系演算语言QBE

    QBE: Query By Example 基于屏幕表格的查询语言 查询要求:以填写表格的方式构造查询 用示例元素(域变量)来表示查询结果可能的情况 查询结果:以表格形式显示 QBE操作框架 ...