这是小川的第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. hdu4352 XHXJ's LIS[数位DP套状压DP+LIS$O(nlogn)$]

    统计$[L,R]$内LIS长度为$k$的数的个数,$Q \le 10000,L,R < 2^{63}-1,k \le 10$. 首先肯定是数位DP.然后考虑怎么做这个dp.如果把$k$记录到状态 ...

  2. 用Python实现的二分查找算法(基于递归函数)

    一.递归的定义 1.什么是递归:在一个函数里在调用这个函数本身 2.最大递归层数做了一个限制:997,但是也可以自己限制 1 def foo(): 2 print(n) 3 n+=1 4 foo(n) ...

  3. SQL手工注入技巧

    MYSQL篇 1.内置函数和变量 @@datadir,version(),database(),user(),load_file(),outfile() 2.利用concat(),group_conc ...

  4. 处理并解决mysql8.0 caching-sha2-password问题,开启远程访问

    原文:https://blog.csdn.net/u010026255/article/details/80062153 启动mysql服务:service mysqld start ALTER US ...

  5. PHP开启错误提示而不是单单返回500

    方法一 修改php.ini文件和php-fpm.conf php.ini文件在我使用的发行版本/etc/php/<版本号>/cli/php.ini php-fpm.conf文件在/etc/ ...

  6. 关于system.timer的使用

    private System.Timers.Timer _timer = null; if (_timer == null) { _timer = new System.Timers.Timer(); ...

  7. 希尔排序java代码

    //希尔排序 通过测试 public class ShellSortTest{ public static void shellSort(int [] arrays){ for(int d=5;d&g ...

  8. [洛谷P4040] AHOI2014 宅男计划

    题目背景 自从迷上了拼图,JYY就变成了个彻底的宅男.为了解决温饱问题,JYY不得不依靠叫外卖来维持生计. 问题描述 外卖店一共有N种食物,分别有1到N编号.第i种食物有固定的价钱Pi和保质期Si.第 ...

  9. C# 使用vs2013 写 windows服务

    第一步:添加windows服务项目 并起一个 好看的名字 第二步:添加安装程序 第三步:右键点击serviceProcessInstaller1属性,在Account中选择LocalSystem 第四 ...

  10. html5 和h5的区别

    html5 是公认的web开发的html规范,是一系列关于html的标准,它就好比是国家的法律,比如未成年不准进网吧,网吧要是允许未成年人进入,国家就要对网吧和未成年人进行处罚和教育.同样的,你写的h ...