这是小川的第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. 多线程之CountDownLatch

    下面请看一个应用场景:有1个driver和5个worker,需要满足以下两点要求: 当driver完成了全部的工作之后才允许worker们开始工作: 当所有的worker都完成了自己的工作之后,dri ...

  2. HihoCoder1076 与链(数位DP)

    时间限制:24000ms 单点时限:3000ms 内存限制:256MB 描述 给定 n 和 k.计算有多少长度为 k 的数组 a1, a2, ..., ak,(0≤ai) 满足: a1 + a2 +  ...

  3. XML的树结构与语法规则

    ㈠概念 什么是 XML? ⑴XML 指可扩展标记语言(EXtensible Markup Language) ⑵XML 是一种标记语言,很类似 HTML ⑶XML 的设计宗旨是传输数据,而非显示数据 ...

  4. 红黑树(RB-tree)比AVL树的优势在哪?

    1. 如果插入一个node引起了树的不平衡,AVL和RB-Tree都是最多只需要2次旋转操作,即两者都是O(1):但是在删除node引起树的不平衡时,最坏情况下,AVL需要维护从被删node到root ...

  5. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  6. 利用gephi作人物网络图

    一.先利用word2vec训练数据得到模型 a.利用jieba对文本进行分词,并只提取词性为人名的词,去除分词长度为1和大于4的词 b.利用word2vec训练分词后的文本,并存储 c.利用训练后的模 ...

  7. Unity3D_(API)射线检测Raycast()

    Unity射线检测官方文档: 传送门 一.检测前方是否有游戏物体(射线无限长度) 二.检测前方是否有游戏物体(射线长度为1m) 三.检测前方游戏物体碰撞信息(射线无限长度): 四.指定检测碰撞Tag层 ...

  8. 客户端框架-MVVM

    MVVM Model-View-ViewModel 如果说MVP是对MVC的进一步改进,那么MVVM则是思想的完全变革.它是将"数据模型数据双向绑定"的思想作为核心,因此在View ...

  9. [CSP-S模拟测试]:城市游戏(图论+DP)

    题目传送门(内部题109) 输入格式 第一行,两个整数$n,m$. 接下来$m$行,每行三个整数$u,v,l$,描述了一条道路连接的两个路口的编号以及道路的长度. 输出格式 输出一行一个整数,为所求的 ...

  10. PriorityQueue源码阅读

    最小堆:优先级权重越小 离顶点越近 案例 实现一个top max n publish static int[] topN(int[] nums, int l){ int[] result = new ...