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

https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89456/short-java-solution-without-using-extra-space

https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89345/Easy-concept-with-PythonC%2B%2BJava-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 406. Queue Reconstruction by Height 根据高度重建队列的更多相关文章

  1. [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 ...

  2. 406 Queue Reconstruction by Height 根据身高重建队列

    假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...

  3. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  4. LN : leetcode 406 Queue Reconstruction by Height

    lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...

  5. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  6. [leetcode] 406. Queue Reconstruction by Height (medium)

    原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...

  7. 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 ...

  8. 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 406. Queue Reconstruction by Height

    一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...

随机推荐

  1. 阿里云CentOS7.x安装nodejs及pm2

    对之前文章的修订 您将了解 CentOS下如何安装nodejs CentOS下如何安装NVM CentOS下如何安装git CentOS下如何安装pm2 适用对象 本文档介绍如何在阿里云CentOS系 ...

  2. JavaScript(这里主要侧重于 JavaScript HTML DOM)杂项

    JavaScript基础 ------学习网站https://www.runoob.com/js/js-tutorial.html Javascript语言的特点: 安全性:2.易用性:3.动态交互性 ...

  3. win10怎么打开服务界面?

    在键盘上同时点击“win+R”键弹出运行界面,输入services.msc,按确定. 这时就弹出了服务的界面,如图:

  4. Python中的passed by assignment与.NET中的passing by reference、passing by value

    Python文档中有一段话: Remember that arguments are passed by assignment in Python. Since assignment just cre ...

  5. Child module D:\program\eclipse\eclipse\workspace_taotao\taotao-parent\taotao-manager-service of

    1.淘淘商城的项目,报了这个错误,也是一脸懵逼. [INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encounte ...

  6. QT+OpenGL(02)-- zlib库的编译

    1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.解压 3.进入  zlib1211\zlib-1.2.11\contrib\vstudio\vc14 目录 ...

  7. pymysql 的简单使用

    一.环境 Windows  7 x64     python 3.7.1    pymysql 0.9.3  mysql5.6.43 二.pymysql的简单使用 1.准备数据库demo_temp c ...

  8. Comet OJ-2019国庆欢乐赛

    国庆玩的有点嗨,开学了补题. A轰炸平面镇魂曲 题目描述 虹村万泰是一位二维世界的替身使者,他的替身 "轰炸平面镇魂曲" 能产生一条直线分割整个平面. 一开始,平面上有一个矩形,其 ...

  9. Django models 单表查询

    从数据库中查询出来的结果一般是一个集合,这个集合叫做 QuerySet 1. 查看Django QuerySet执行的SQL .query.__str__()或 .query属性打印执行的sql语句 ...

  10. Tomcat部署项目时,发布的项目页面部分乱码,且页面渲染文件也是乱码。

    catalina.bat中必须设置为UTF-8,如果我不设置为UTF-8,页面接收到的就是乱码了,尝试过各种UTF-8的调试,都无解,最后还是只能在catalina.bat的set "JAV ...