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次移动(注意每次移动会增加两个元素 ...
随机推荐
- K-means聚类分析MATLAB代码
function kmeans load q1x.dat; a1=round(98*rand+1); a2=round(98*rand+1); miao1=[q1x(a1,1),q1x(a1,2)]; ...
- 21天学通C++_Day3_Part1
今天奔跑吧兄弟来杭电了,决定不去看,宅在科协继续啃(反正去了也看不到咯),继续继续,今天白天没课,希望可以更两个. 0.C风格字符串 在数组中间把某个字符替换成‘\0’并不会改变数组的长度,但是显示时 ...
- Storm开发过程中的问题与建议
转自:http://blog.csdn.net/ouyang111222/article/details/50061305 (一) topology层级建议设不要设置过多 storm讲究是流式计算,s ...
- 【java反射】Class类型的相关操作演练
[一]获取范型接口的实现类的范型类型 (1)范型接口 package org.springframework.context; import java.util.EventListener; publ ...
- 使用Spring的StingUtils的commaDelimitedListToStringArray来获取字符串数组
有时候我们需要把一些带逗号的字符串解析为数组或者List.Set,如果自己转换的话比较麻烦,可以直接使用Spring的工具类StringUtils,通过配置文件调用该工具类方法即可.具体看例子 < ...
- css学习笔记之图像
图像与文本的对齐方式: vertical-align:text-top;表示的意思是图像的顶部和同一行的文本对齐,但文本不会超出图片的上边线. vertical-align:middle;表示的意思是 ...
- 安装ruby-pg报错解决
ruby用pgsql做orm的时候,需要安装ruby-pg库,默认编译安装会提示缺少xx头文件 max下,我用的傻瓜式pgsql.app gem install pg -- --with-pg-con ...
- phantomjs 安装和试用
准备学习casperjs, 发现官网上说 it’s an extremely useful companion to PhantomJS, 所以决定下把它下来试试.下载安装(win7)没什么可说的, ...
- Hadoop入门介绍一
Hadoop1.是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.2.Hadoop实现了一个分布式文件 ...
- 安全测试回顾(一)补充:burp 的基本操作
浏览器设置; 拦截请求: Spider 伪造请求: 对这个url 伪造请求 拦截一个页面后,进入攻击模块 sniper 两个参数值 保证一个不变 另一个 进行枚举 battering ram 两个 ...