LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height
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]]
贪心算法 Accepted
首先,利用sort函数对people数组进行排序,使其中的顺序变为高度最高的在最前面,当高度一样时,令k值小的排在前面,优先满足最高且k最小的人的需求。
最后,依次将人插入到当前ans第k+1个的位置,得到的ans数组即为所求排序。
正确性解释:最高且k较小的人具有更高的优先性,因为当其已被插入,之后再插入的人,对在他之前比他高或相等的人的个数没有影响。
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
auto cmp = [](pair<int, int>& p1, pair<int, int>& p2) {
return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second);
};
sort(people.begin(), people.end(), cmp);
vector<pair<int, int>> ans;
for (auto&p : people) ans.insert(ans.begin()+p.second, p);
return ans;
}
};
LN : leetcode 406 Queue Reconstruction by Height的更多相关文章
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- [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 ...
- [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的做法和剪 ...
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
随机推荐
- mysql你确定掌握的那些sql语句
1.创建表 create table test(uid int not null,create_time timestamp default current_timestamp); 即:没有双引号,单 ...
- php文件上传判断类型
上传文件对象在$_FILES['Filedata']对象中,临时路径是tmp_name,判断是上传文件是否为真实图片方法很多,我用的是这个: if( !@getimagesize( $_FILES[' ...
- android 加密手机完毕后待机两分钟出现有频率的杂音
这个音效是code里面主动加的,是为了提醒end user输入PIN的一个提示音,也标志着加密手机动作的完毕. 详细位置是在alps\packages\apps\Settings\src\com\an ...
- Linux/UNIX之进程环境
进程环境 进程终止 有8种方式使进程终止,当中5中为正常终止,它们是 1) 从main返回 2) 调用exit 3) 调用_exit或_Exit 4) 最后一个 ...
- centos内核编译配置
1.下载内核,下面是内核官网地址:https://www.kernel.org/ tar.xz 是完整的内核包(完整的内核包是通过两次压缩得到的.一次是xz,一次是tar) pgp 是 ...
- nginxserver报403 forbidden错误的解决的方法
改动nginx.config文件内容: location / { #root html; root D:\java; ...
- input title 悬浮值
<!doctype html><html lang="en"> <head> <meta charset="UTF-8&quo ...
- Robot Framework 怎样写好Test Case
1.介绍 这是一个关于如何用Robot Framework写好Test Case的高层次的指导准则 怎样实际的与系统进行交互不在此文档范围内 最重要的准则是使测试用例尽可能的让熟悉此领域的人觉得简单易 ...
- go15---select
package main import ( "fmt" ) //go语言提供了一个结构或者形式来帮助处理多个channel的发送和接收问题,这个结构叫做select, //sele ...
- APDU报文【转】
本文转载自:http://www.cnbolgs.com/snail0404/p/5436348.html APDU # APDU # 定义:APDU(ApplicationProtocolDat ...