这是小川的第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)的更多相关文章

  1. LeetCode 1051. 高度检查器(Height Checker) 28

    1051. 高度检查器 1051. Height Checker 题目描述 学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列. 请你返回至少有多少个学生没有站在正确位置数量.该人数指的是 ...

  2. Leetcode 1051. 高度检查器

    这题的目的是找出排序后和排序前位置不同的元素的个数 正常通过复制出一个新的数组,然后对比排序后的数组就能做出,但是时间是1ms 然后发现一种基于桶排序来计数的做法 public int heightC ...

  3. 【LEETCODE】40、1051. Height Checker

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 【Leetcode_easy】1051. Height Checker

    problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>&a ...

  5. [Swift]LeetCode966.元音拼写检查器 | Vowel Spellchecker

    Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word ...

  6. SOAPUI使用教程-WSDL项目---检查器

    SoapUI Pro添加了许多可用的WSDL消息上下文的检查器. XSD / XML Schema检查器 XML Schema检查器显示当前节点对应的XML模式定义. 下面的屏幕截图显示了在Bing搜 ...

  7. python 拼写检查代码(怎样写一个拼写检查器)

    原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...

  8. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  9. SQL Server 2005 无法连接到WMI提供程序 无法执行 SQL Server 系统配置检查器

    无法连接到WMI提供程序.你没有权限或者该服务器无法访问/cannot connect to WMI provider. You do not have permission or the--由于计算 ...

随机推荐

  1. 第二章 Vue快速入门-- 26 过滤器-定义私有过滤器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. Spring JdbcTemplate + transactionTemplate 简单示例 (零配置)

    jdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...

  3. C# 常用方法——图片转base64字符串

    其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html /// <summary> /// Image 转成 base64 / ...

  4. json的值键对,对象,数组,逻辑值

    详细说一下有关json的相关知识: ㈠json与xml的异同 ★与 XML 相同之处 ⑴JSON 是纯文本 ⑵JSON 具有"自我描述性"(人类可读) ⑶JSON 具有层级结构(值 ...

  5. 字典树Trie--实现敏感词过滤

    序言 Trie树 资料 https://blog.csdn.net/m0_37907797/article/details/103272967?utm_source=apphttps://blog.c ...

  6. Python天天学_01_基础1

    Python_day_01 金角大王:http://www.cnblogs.com/alex3714/articles/5465198.html ------Python是一个优雅的大姐姐 学习方式: ...

  7. Confluence 6 预览一个文件

    当你浏览一个页面的时候,单击一个图片,文件缩略图或者链接将会运行预览. 预览视图包括了从远程 Web 页面导入的图片文件和已经附加到页面中的文件(尽管有可能这些文件没有在页面中显示). 在预览中你可以 ...

  8. AcWing:138. 兔子与兔子(字符串Hash)

    很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然后我们每 ...

  9. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

  10. 如何快速查询中科院JCR分区和汤森路透JCR分区

    参考: https://blog.csdn.net/chichuhe/article/details/83054624 https://blog.csdn.net/Sunflower02/articl ...