LeetCode406 queue-reconstruction-by-height详解
题目详情
假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。
注意:
总人数少于1100人。
示例
输入:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
输出:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
解题思路
此题放在贪心算法中, 是用贪心的思想来解题
首先对数组 按照第一个元素height 降序排列
此时 数组前面的元素的height 大于后面元素的height
然后 以每个元素的k值 插入到返回数组里, 比如元素的k为 2, 就插到 返回数组的下标为2的位置。
以题目的例子来进行操作
这是原来的数组
people = [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
按照height进行降序排序之后
people = [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
对降序后的people数组 从第一个元素开始 把每个元素加入到返回数组
加入的策略是, 按照每个元素的k 来插入到下表为k的位置上
1.
插入 [7,0]
插入到离开始位置偏移了0个距离的位置。
result = [[7,0]]
2.
插入 [7,1]
插入到离开始位置偏移了1个距离的位置,即插入到[7,0]的后面。
result = [[7,0], [7,1]]
3.
插入 [6,1]
插入到离开始位置偏移了1个距离的位置,即插入到[7,0]的后面。
result = [[7,0], [6,1], [7,1]]
4.
插入 [5,0]
插入到离开始位置偏移了0个距离的位置,即插入到[7,0]的前面。
result = [[5,0], [7,0], [6,1], [7,1]]
5.
插入 [5,2]
插入到离开始位置偏移了2个距离的位置,即插入到[7,0]的后面。
result = [[5,0], [7,0], [5,2], [6,1], [7,1]]
6.
插入 [4,4]
插入到离开始位置偏移了4个距离的位置,即插入到[6,1]的后面。
result = [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
得到最终结果
result: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
贪心证明
把每个元素以k 插进去, 因为是按照height 来由大到小插进去的, 把元素插进第k位置, 插入后前面的height 是比当前插入元素大(因为已经插入的元素height大或者相等), 这就满足了排在前面身高大于或等于的人数为k, 但是要注意 我现在说的数组 并不是最终结果的数组, 而是插入这个元素形成的中间数组。 是相对于 中间数组来说的。
那么有一个问题了, 最终结果里 [6,1] 前面有[5,0], [5,1], 这些height是小于6的, 但是排在[6,1]前面, 这些元素是在[6,1]加进去之后加进去的, 这些加进去的元素对 [6,1] 满足的条件(排在前面身高大于或等于的人数为k) 会有影响吗? 答案是不会的,因为在[6,1]加进去的元素的height 是小于6的 无论加在哪个地方对 [6,1]的 k 不会改变。
综上, 此算法能求出结果
AC代码
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
vector<pair<int, int>> result;
// lambda 表达式
sort(people.begin(), people.end(), [](pair<int, int> a, pair<int, int> b){
return a.first > b.first || (a.first == b.first && a.second < b.second);
});
for (auto i: people) {
result.insert(result.begin() + i.second, i);
}
return result;
}
};
总结
因为在前面贪心的过程中 插入的元素不会对前面已经插入元素的k造成印象, 所以才能贪心, 否则 就可能用到其他的方法来解题了。
LeetCode406 queue-reconstruction-by-height详解的更多相关文章
- LeetCode406. Queue Reconstruction by Height Add to List
Description Suppose you have a random list of people standing in a queue. Each person is described b ...
- 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) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- 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 ...
- [Swift]LeetCode406. 根据身高重建队列 | 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] 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 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- 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
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
随机推荐
- Ethical Hacking - NETWORK PENETRATION TESTING(17)
MITM - bypassing HTTPS Most websites use https in their login pages, this means that these pages are ...
- Python Ethical Hacking - BACKDOORS(5)
File Download: A file is a series of characters. Therefore to transfer a file we need to: 1. Read th ...
- P2058 海港 (洛谷)
这个题复制过来真的有点恶心,懒得手打,以后再搬题面吧. 今天我双更了,AC这个题我就完成某谷春令营第一课的作业了(假的) 这个题是个双指针.非常友善.一直往里读入就可以了,遇见不是一条船的乘客输出这一 ...
- node name配置错误,导致grid日志在报警
[root@aipdb ContentsXML]# cat inventory.xml <?xml version="1.0" standalone="yes&qu ...
- vue 表格使用el-select
<el-table-column label="示例" width="210" align="center"> <temp ...
- list基本使用
list和vector的用法基本相同,区别如下: list可以头尾插入和删除,效率一样,vector只有尾部插入和删除效率才高,头部操作效率很低 list的排序有专有的接口,不能使用全局的接口,原因是 ...
- git常用命令操作
git常用命令 #查看配置 git config -l #查看系统config git config --system --list #查看当前用户(global)配置 git config --gl ...
- 从RNN到BERT
一.文本特征编码 1. 标量编码 美国:1 中国:2 印度:3 … 朝鲜:197 标量编码问题:美国 + 中国 = 3 = 印度 2. One-hot编码 美国:[1,0,0,0,…,0]中国:[0, ...
- MYSQL的事物四大特性
MYSQL的事物四大特性(ACID) 1.什么是事物? 事务(Transaction)是并发控制的基本单位.所谓的事务,它是由单独单元的一个或者多个sql语句组成,在这个单元中,每个mysql语句是相 ...
- 因为不知道Java的CopyOnWriteArrayList,面试官让我回去等通知
先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有一线大厂整理的面试题,以及我的系列文章. ...