LeetCode.1051-身高检查器(Height Checker)
这是小川的第390次更新,第420篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第252题(顺位题号是1051)。要求学生按身高递增的顺序站列来拍年度照片。
返回没有站在正确位置的学生人数。(这是必须移动的学生人数,以便所有学生身高能够以递增的顺序排列。)
例如:
输入:[1,1,4,2,1,3]
输出:3
说明:身高4,3和最后身高1的学生没有站在正确的位置。
注意:
1 <= heights.length <= 100
1 <= heights[i] <= 100
02 第一种解法
题目很简单,找出没有按高度增序站的学生人数即可。
思路:将原数组的值复制一份出来,将其排序,使用Arrays类的sort方法完成,再与原数组对应位置的元素比较,值不相等就加1,最后返回总人数。
时间复杂度为O(N log(N)),空间复杂度为O(N)。
public int heightChecker(int[] heights) {
int count = 0, n = heights.length;
int[] copy = Arrays.copyOf(heights, n);
Arrays.sort(copy);
for (int i=0; i<n; i++) {
if (heights[i] != copy[i]) {
count++;
}
}
return count;
}
03 第二种解法
还记得前几天详细介绍过的计数排序吗?此题就可以用上,此解法是简单版实现。
时间复杂度为O(N),空间复杂度为O(N)。
public int heightChecker2(int[] heights) {
int[] count = new int[101];
for (int num : heights) {
count[num]++;
}
int n = heights.length, index = 0;
int[] result = new int[n];
for (int i=0; i<count.length; i++) {
while (count[i] > 0) {
result[index++] = i;
count[i]--;
}
}
int times = 0;
for (int i=0; i<n; i++) {
if (heights[i] != result[i]) {
times++;
}
}
return times;
}
04 第三种解法
进阶版的计数排序算法实现。
时间复杂度为O(N),空间复杂度为O(N)。
public int heightChecker3(int[] heights) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int num : heights) {
min = Math.min(min, num);
max = Math.max(num, max);
}
int[] count = new int[max-min+1+1];
for (int num : heights) {
count[num-min+1]++;
}
for (int i=1; i<count.length; i++) {
count[i] += count[i-1];
}
int n = heights.length;
int[] result = new int[n];
for (int j=0; j<n; j++) {
result[count[heights[j]-min]++] = heights[j];
}
int times = 0;
for (int i=0; i<n; i++) {
if (heights[i] != result[i]) {
times++;
}
}
return times;
}
05 第四种解法
针对简版的计数排序算法,我们还可以再简化下,在将排序后的元素填充到新数组时,顺便与原数组的对应元素进行比较,省掉后面单独再开一个for循环来比较。
时间复杂度为O(N),空间复杂度为O(N)。
public int heightChecker4(int[] heights) {
int[] count = new int[101];
for (int num : heights) {
count[num]++;
}
int n = heights.length, index = 0;
int times = 0;
int[] result = new int[n];
for (int i=0; i<count.length; i++) {
while (count[i] > 0) {
result[index] = i;
if (heights[index] != result[index]) {
times++;
}
index++;
count[i]--;
}
}
return times;
}
06 小结
算法专题目前已连续日更超过七个月,算法题文章258+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.1051-身高检查器(Height Checker)的更多相关文章
- LeetCode 1051. 高度检查器(Height Checker) 28
1051. 高度检查器 1051. Height Checker 题目描述 学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列. 请你返回至少有多少个学生没有站在正确位置数量.该人数指的是 ...
- Leetcode 1051. 高度检查器
这题的目的是找出排序后和排序前位置不同的元素的个数 正常通过复制出一个新的数组,然后对比排序后的数组就能做出,但是时间是1ms 然后发现一种基于桶排序来计数的做法 public int heightC ...
- 【LEETCODE】40、1051. Height Checker
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】1051. Height Checker
problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>&a ...
- [Swift]LeetCode966.元音拼写检查器 | Vowel Spellchecker
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word ...
- SOAPUI使用教程-WSDL项目---检查器
SoapUI Pro添加了许多可用的WSDL消息上下文的检查器. XSD / XML Schema检查器 XML Schema检查器显示当前节点对应的XML模式定义. 下面的屏幕截图显示了在Bing搜 ...
- python 拼写检查代码(怎样写一个拼写检查器)
原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- SQL Server 2005 无法连接到WMI提供程序 无法执行 SQL Server 系统配置检查器
无法连接到WMI提供程序.你没有权限或者该服务器无法访问/cannot connect to WMI provider. You do not have permission or the--由于计算 ...
随机推荐
- 关于客户端连接mysql的授权问题
mysql远程连接 Host * is not allowed to connect to this MySQL server的错误. 是因为mysql需要授权才能访问.授权方式: 授权给某一个ip: ...
- 【C#-枚举】枚举的使用
枚举是用户定义的整数类型. namespace ConsoleApplication1 { /// <summary> /// 在枚举中使用一个整数值,来表示一天的阶段 /// 如:Tim ...
- [Linux系统] (8)Nginx
一.高并发基础架构 简要流程: 1.客户端发请求. 2.又LVS等四层负载均衡系统将请求转发给不同的Nginx服务器. 3.Nginx与客户端建立TCP连接,拿到请求后分析URI,然后将其转发给对应的 ...
- JavaScript 输出的四种方法
JavaScript 没有任何打印或者输出的函数. ㈠JavaScript 显示数据 ⑴使用 window.alert() 弹出警告框. ⑵使用 document.write() 方法将内容写到 HT ...
- Spring实例化相关问题
1.当Controller或者Service使用new来实例化时,能不能正常调用使用Resource声明的变量 不能,使用new来实例化时,所有使用Resource声明的变量均为null
- oracle判断一个字段为空
比如 insert into table a (a1,b1)values("a1",''); 对于这种情况,因为表里存的是'',其实是没有内容的,要查询这个字段,不能直接使用 se ...
- d3.js+svg的树形图
效果图 数据 { "name":"中国", "children": [ { "name":"浙江" ...
- shell定义
用户输入的命令并且把它们送到内核.不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序. Shell编程语言具有普通编程语言的很多特点 无图形化界面时与lin ...
- SQL Server清空数据库中ldf日志文件
USE [master] ALTER DATABASE [Whir_InternalSystem] SET RECOVERY SIMPLE WITH NO_WAIT ALTER DATABASE [W ...
- 【Spark机器学习速成宝典】模型篇01支持向量机【SVM】(Python版)
目录 支持向量机原理 支持向量机代码(Spark Python) 支持向量机原理 详见博文:http://www.cnblogs.com/itmorn/p/8011587.html 返回目录 支持向量 ...