453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的
[抄题]:
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个数,变成统一的的更多相关文章
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 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 ...
- 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 ...
- 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 ...
- 453. Minimum Moves to Equal Array Elements
Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...
- [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 ...
- 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 ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 453 Minimum Moves to Equal Array Elements 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1.示例:输入:[1,2,3]输出:3解释:只需要3次移动(注意每次移动会增加两个元素 ...
随机推荐
- hibernate的list和iterate的区别
一.先介绍一下java中的缓存系统JCS(java cache system) 1.JCS(Java Caching System)是一个对象Cache,它可以把Java对象缓存起来,提高那些访问频 ...
- 关于niosii不同版本的ip核不兼容的问题
这次用到网上下载的一个12.0版本的ip核,使用qsys做的,而我的开发环境是10.1的,sopc是用的sopcbuilder做的,下载下来的ip核添加新组建后,会报错,采取的做法是对比我的10.1版 ...
- 十四、python沉淀之路--文件操作
一.文件操作b模式 1. # f = open('test11.py','rb',encoding='utf-8') # 这种情况会报错 f = open('test11.py','rb') # b ...
- mysql时间运算
DELETE FROM zhongqiu WHERE caretatime<=ADDDATE(NOW(),-14) caretatime` timestamp NOT NULL DEFAULT ...
- php://input解决APP发送图片问题
今天公司要求用APP发送一个图片到PHP程序接收并保存起来,而且中间还需要很多参数! 以前没有做过APP和PHP交互,这次算是一个挑战吧(对一个没有人指导实习生来说) 1.APP发1.jpg,而且带有 ...
- plsql无法连接64位oracle数据库的解决方法
今儿个重装了个系统,win8 64位.接着装了个64位的oracle11g,oracle11g下载页面:http://www.oracle.com/technetwork/database/enter ...
- 关于yii2 REST api 的问题
首先,需要在basic/web/文件夹下添加一个.htaccess文件 这样进入项目就会自动访问index.php文件,url就不会错乱了 <IfModule mod_rewrite.c> ...
- 新机器连接老机器遇到的ERROR
Ansible无法连接老旧机器 报错内容: [root@BI ansible]# ansible -i /etc/ansible/hosts GameServer -m ping 10.10.113. ...
- selenium定位方法
- pytest命令行选项
-m 标记 代码加一个装饰器:@pytest.mark.run_bbc_test,命令行添加 -m run_bbc_test,执行带@pytest.mark.run_bbc_test的测试用例: -k ...