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. 【Linux命令】Linux压缩及解压命令

    Linux压缩及解压命令 一.文件打包和压缩命令介绍 linux系统文件压缩格式,常用的有*.tar.gz.*.gz.*.zip.*.tar,还有*.rar..7z..bz2..tar.xz..tar ...

  2. 如何在 C# 中自定义 Comparer,以实现按中文拼音(a-z)来排序

    1. 为何要自定义 Comparer a. 先看如下代码 class Program { public static void Main(string[] args) { List<string ...

  3. 机器学习(八)--------支持向量机 (Support Vector Machines)

    与逻辑回归和神经网络相比,支持向量机或者简称 SVM,更为强大. 人们有时将支持向量机看作是大间距分类器. 这是我的支持向量机模型代价函数 这样将得到一个更好的决策边界 理解支持向量机模型的做法,即努 ...

  4. ansible碎碎念

    1. Using a SSH password instead of a key is not possible because Host Key checking is enabled and ss ...

  5. Logstash:多个配置文件(conf)

    Logstash:多个配置文件(conf) 对于多个配置的处理方法,有多个处理方法: 多个pipeline 一个pipleline处理多个配置文件 一个pipeline含有一个逻辑的数据流,它从inp ...

  6. 如何取消 SqlDataAdapter.Fill() 的执行(转载)

    问 Scenario: We have a DataGridView which is attached to DataAdapter (datatable), we load the data in ...

  7. 用ASP.NET创建数据库

    小白的第一次使用: 程序员写程序,就好比一个物品的慢慢诞生,我们今天的这个例子就可以想象成一个物品慢慢的在编译的过程中,让我们所看到 一.创建我们所测试的项目 1.创建一个简单的带有模型层(Model ...

  8. 死磕Synchronized底层实现,面试你还怕什么?

    关于synchronized的底层实现,网上有很多文章了.但是很多文章要么作者根本没看代码,仅仅是根据网上其他文章总结.照搬而成,难免有些错误:要么很多点都是一笔带过,对于为什么这样实现没有一个说法, ...

  9. pip下载速度慢解决方法

    添加镜像链接 解决方式: 更改pip的数据源.目前国内比较知名的有豆瓣的,清华的.都是pipy官网的镜像. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里 ...

  10. IOC : Unity 配置和使用

    原文出自:IOC : Unity 配置和使用 之前Terry Lee 已经介绍过Unity的简单使用了,不过那篇文章是针对旧版本的,现在的版本1.2版略有不同. 我下载了Unity并做了一个简单的测试 ...