[LeetCode] 406. Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k)
, where h
is the height of the person and k
is the number of people in front of this person who have a height greater than or equal to h
. Write an algorithm to reconstruct the queue.
Note:
The number of people is less than 1,100.
Example
Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
这道题给了我们一个队列,队列中的每个元素是一个 pair,分别为身高和前面身高不低于当前身高的人的个数,让我们重新排列队列,使得每个 pair 的第二个参数都满足题意。首先来看一种超级简洁的方法,给队列先排个序,按照身高高的排前面,如果身高相同,则第二个数小的排前面。然后新建一个空的数组,遍历之前排好序的数组,然后根据每个元素的第二个数字,将其插入到 res 数组中对应的位置,参见代码如下:
解法一:
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), [](vector<int>& a, vector<int>& b) {
return a[] > b[] || (a[] == b[] && a[] < b[]);
});
vector<vector<int>> res;
for (auto a : people) {
res.insert(res.begin() + a[], a);
}
return res;
}
};
上面那种方法是简洁,但是用到了额外空间,我们来看一种不使用额外空间的解法,这种方法没有使用 vector 自带的 insert 或者 erase 函数,而是通过一个变量 cnt 和k的关系来将元素向前移动到正确位置,移动到方法是通过每次跟前面的元素交换位置,使用题目中给的例子来演示过程:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
排序后:
[[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
交换顺序:
[[7,0], [6,1], [7,1], [5,0], [5,2], [4,4]]
[[5,0], [7,0], [6,1], [7,1], [5,2], [4,4]]
[[5,0], [7,0], [5,2], [6,1], [7,1], [4,4]]
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
解法二:
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), [](vector<int>& a, vector<int>& b) {
return a[] > b[] || (a[] == b[] && a[] < b[]);
});
for (int i = ; i < people.size(); ++i) {
int cnt = ;
for (int j = ; j < i; ++j) {
if (cnt == people[i][]) {
auto t = people[i];
for (int k = i - ; k >= j; --k) {
people[k + ] = people[k];
}
people[j] = t;
break;
}
++cnt;
}
}
return people;
}
};
下面这种解法跟解法一很相似,只不过没有使用额外空间,而是直接把位置不对的元素从原数组中删除,直接加入到正确的位置上,参见代码如下:
解法三:
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), [](vector<int>& a, vector<int>& b) {
return a[] > b[] || (a[] == b[] && a[] < b[]);
});
for (int i = ; i < people.size(); i++) {
auto p = people[i];
if (p[] != i) {
people.erase(people.begin() + i);
people.insert(people.begin() + p[], p);
}
}
return people;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/406
类似题目:
Count of Smaller Numbers After Self
参考资料:
https://leetcode.com/problems/queue-reconstruction-by-height/
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89348/6-lines-Concise-C%2B%2B
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 406. Queue Reconstruction by Height 根据高度重建队列的更多相关文章
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...
- [leetcode] 406. Queue Reconstruction by Height
https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...
- [leetcode] 406. Queue Reconstruction by Height (medium)
原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...
- LC 406. Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 406. Queue Reconstruction by Height
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
随机推荐
- CSS3倒影效果
比较简单的倒影效果 <pre><div class="box-reflect"><img src="https://www.baidu.co ...
- php json_decode无法处理\解决方法
php json_decode无法处理\解决方法 <pre>$aa=urlencode('eee\ee');$dfda='[{"company":"测试&qu ...
- linux bash 的基础语法
示例均来自网络,附带有原始链接地址,自己练习整理发出,均测试可用 linux shell 基本语法 - 周学伟 - 博客园 https://www.cnblogs.com/zxouxuewei/p/6 ...
- SpringBoot多数据源动态切换数据源
1.配置多数据源 spring: datasource: master: password: erp_test@abc url: jdbc:mysql://127.0.0.1:3306/M201911 ...
- C# - MD5验证
前言 本篇主要记录:VS2019 WinFrm桌面应用程序实现字符串和文件的Md5转换功能.后续系统用户登录密码保护,可采用MD5加密保存到后台数据库. 准备工作 搭建WinFrm前台界面 如下图 核 ...
- badboy录制过程中出现当前页面的脚本发现错误
为什么出现这个提示 , 是因为访问者使用的浏览器不能完全支持页面里的脚本,毕竟版本太老,一直没有更新 ,这个版本错误并不会影响使用,有强迫症的可以关闭下,
- Linux文本文件——文本编辑器Vim
Linux文本文件——文本编辑器Vim 摘要:本文主要学习在Linux系统中使用Vim文本编辑器编辑文本. 什么是Vim Vim是一个基于文本界面的编辑工具,使用简单且功能强大.更重要的是,Vim是所 ...
- JMM和Volatile底层原理分析
JMM和volatile分析 1.JMM:Java Memory Model,java线程内存模型 JMM:它是一个抽象的概念,描述的是线程和内存间的通信,java线程内存模型和CPU缓存模型类似,它 ...
- Vue中iframe和组件的通信
最近的项目开发中用到了Vue组件中嵌套iframe,相应的碰到了组件和HTML的通信问题,场景如下:demo.vue中嵌入 test.html 由于一般的iframe嵌套是用于HTML文件的,在vue ...
- CTF必备技能丨Linux Pwn入门教程——调整栈帧的技巧
Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...