[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的做法和剪 ...
随机推荐
- 一位IT民工的十年风雨历程
距离2020年只有30天了,转眼毕业快10年. 回首自己,已三十有三,中年危机. 古人云三十而立,我却还在测试途中摸爬滚打. 创业,自由职业永远是一个梦,白日梦. 焦虑.迷茫.看不到希望. 这两天一场 ...
- java 金额数字转换大写算法
根据人民币大写金额规范,转换有几点要注意的: 阿拉伯数字中间有"0"时,中文大写金额中间可以只写一个"零"字.如¥1,409.50,应写成人民币壹仟肆佰零玖圆伍 ...
- oracle的instr()函数
我们知道很多语言都提供了indexOf()和lastIndexOf()函数,以便能查找某个字符在某个字符串中的出现的位置和最后一次出现的位置. 但是Oracle没有提供这两个函数,事实上,它提供了一个 ...
- virsh console配置
If you're trying to get to the console, you can either use virt-viewer for the graphical console or ...
- Jms规范学习
1.什么是消息中间件? 关注于数据的发送和接受,利用高效可靠的异步消息传递机制集成分布式系统. 2.什么是JMS? Java消息服务(java Message Service)即JMS,是一个java ...
- 编译 datax
datax 是阿里巴巴官方开源的一个数据同步工具,可以用于诸多数据源之间的同步,并且使用简单.效率高. datax 官方有提供编译好的版本,可以直接下载,但是其中包含有 BUG. 我最近遇到的一个问题 ...
- jenkins+robotframework邮件发送报告模板
1.Jenkins中配置系统邮件系统管理–系统设置,配置Extended E-mail Notification 2.jenkins 创建一个新项目,项目创建成功,配置邮件
- 高精度gcd
#include<iostream> #include<cstdio> #include<cstring> #define inf 1000000000 using ...
- deepin把vscode设为默认文本应用
一开始我想自己写一个desktop文件放在/usr/share/applications下面,结果在右键菜单里面找不到vscode. [Desktop Entry] Categories=Develo ...
- Java基础回顾——反射+案例
一,引言: 从大二刚开始接触java,了解其基本语法,到用swing编写图形界面的程序,到JavaWeb用SSH三大框架编写小网站,后面又学SSM,SpringBoot,SpringCloud等.可以 ...