这是小川的第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)

  1. public int heightChecker(int[] heights) {
  2. int count = 0, n = heights.length;
  3. int[] copy = Arrays.copyOf(heights, n);
  4. Arrays.sort(copy);
  5. for (int i=0; i<n; i++) {
  6. if (heights[i] != copy[i]) {
  7. count++;
  8. }
  9. }
  10. return count;
  11. }

03 第二种解法

还记得前几天详细介绍过的计数排序吗?此题就可以用上,此解法是简单版实现。

时间复杂度为O(N),空间复杂度为O(N)

  1. public int heightChecker2(int[] heights) {
  2. int[] count = new int[101];
  3. for (int num : heights) {
  4. count[num]++;
  5. }
  6. int n = heights.length, index = 0;
  7. int[] result = new int[n];
  8. for (int i=0; i<count.length; i++) {
  9. while (count[i] > 0) {
  10. result[index++] = i;
  11. count[i]--;
  12. }
  13. }
  14. int times = 0;
  15. for (int i=0; i<n; i++) {
  16. if (heights[i] != result[i]) {
  17. times++;
  18. }
  19. }
  20. return times;
  21. }

04 第三种解法

进阶版的计数排序算法实现。

时间复杂度为O(N),空间复杂度为O(N)

  1. public int heightChecker3(int[] heights) {
  2. int min = Integer.MAX_VALUE;
  3. int max = Integer.MIN_VALUE;
  4. for (int num : heights) {
  5. min = Math.min(min, num);
  6. max = Math.max(num, max);
  7. }
  8. int[] count = new int[max-min+1+1];
  9. for (int num : heights) {
  10. count[num-min+1]++;
  11. }
  12. for (int i=1; i<count.length; i++) {
  13. count[i] += count[i-1];
  14. }
  15. int n = heights.length;
  16. int[] result = new int[n];
  17. for (int j=0; j<n; j++) {
  18. result[count[heights[j]-min]++] = heights[j];
  19. }
  20. int times = 0;
  21. for (int i=0; i<n; i++) {
  22. if (heights[i] != result[i]) {
  23. times++;
  24. }
  25. }
  26. return times;
  27. }

05 第四种解法

针对简版的计数排序算法,我们还可以再简化下,在将排序后的元素填充到新数组时,顺便与原数组的对应元素进行比较,省掉后面单独再开一个for循环来比较。

时间复杂度为O(N),空间复杂度为O(N)

  1. public int heightChecker4(int[] heights) {
  2. int[] count = new int[101];
  3. for (int num : heights) {
  4. count[num]++;
  5. }
  6. int n = heights.length, index = 0;
  7. int times = 0;
  8. int[] result = new int[n];
  9. for (int i=0; i<count.length; i++) {
  10. while (count[i] > 0) {
  11. result[index] = i;
  12. if (heights[index] != result[index]) {
  13. times++;
  14. }
  15. index++;
  16. count[i]--;
  17. }
  18. }
  19. return times;
  20. }

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. UVA - 11996 Jewel Magic (Treap+二分哈希)

    维护一个01序列,一共四种操作: 1.插入一个数 2.删除一个数 3.反转一个区间 4.查询两个后缀的LCP 用Splay或者Treap都可以做,维护哈希值,二分求LCP即可. 注意反转序列的时候序列 ...

  2. RocketMQ和Kafka的差异对比

    Broker差异 主从差异: kafka的master/slave是基于partition维度的,而rocketmq是基于broker维度的:kafka的master/slave是可以切换的,而roc ...

  3. 手机端 设置html上font-size的值 使用rem

    在head标签上加入: (function() { var b = navigator.userAgent; ipad = b.match(/(iPad).*OS\s([\d_]+)/) ? true ...

  4. 初识容器和Docker

    什么是Docker? Docker 是一个用于开发,交付和运行应用程序的开放平台.能够就应用程序和基础架构分开,从而可以快速的交付软件. 借助Docker可以和管理应用程序的方式来管理基础架构. 使用 ...

  5. 清空DataGridView

    DataTable dt = (DataTable)dgv.DataSource; dt.Rows.Clear(); dgv.DataSource = dt;

  6. 【JZOJ5180】【NOI2017模拟6.29】呵呵

    题目 分析 套上prufer序列, 对于一颗n个节点度数分别为\(d_1.d_2...d_n\)方案数为\(\dfrac{(n-2)!}{(d_1-1)!(d_2-1)!......(d_n-1)!} ...

  7. layui的数据表格加上操作

    数据表格加上操作. <script type="text/html" id="barDemo"> <a class="layui-b ...

  8. OpenCV笔记(1)(图片读取与现实、色彩空间、基础运算、均值方差、逻辑运算、泛洪填充、均值中值及自定义平滑)

    一.图片读取和显示 import cv2 as cv # 图片读取cv.imread(img_path) car_img = cv.imread("car1.png") # 图片显 ...

  9. maven项目创建4 dao层整合

    项目配置文件要放在打包成war包的web项目中 创建文件步骤 1    SqlMapConfig.xml <?xml version="1.0" encoding=" ...

  10. CF 354 D 迷宫与门的旋转 BFS +状态压缩 一定要回头看看

    D. Theseus and labyrinth time limit per test 3 seconds memory limit per test 256 megabytes input sta ...