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的更多相关文章

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

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

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

  3. [leetcode] 406. Queue Reconstruction by Height

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

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

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

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

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

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

  7. 406. Queue Reconstruction by Height

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

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

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

  9. LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46

    406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...

随机推荐

  1. springboot-jjwt HS256加解密(PS:验证就是解密)

    最近项目需要用到类似access token进行加解密.验签的需求,本人在此做个小笔记记录一下,以供他人参考. 一共会用到2中加解密,HS256 和 RS256,本文只是对 HS256做个备注,好了直 ...

  2. JavaScript为字符串添加样式

    <script type="text/javascript"> var txt="Hello World!" document.write(&quo ...

  3. 洛谷 P4136 谁能赢呢?

    P4136 谁能赢呢? 题目描述 小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移 ...

  4. mysql利用timestamp来进行帖子排序

    select * from table order by timestamp descorder by 该列 desc timestamp字段也可以用来排序,对应Java类型的.net.timesta ...

  5. delphi的一些语法知识 以及参数传递问题,按引用方式传递参数,按值方式传递参数

    //delphi中exit,abort,break,continue 的区别 exit: 退出函数体abort: 遇到异常,安静处理,就是不显示不提示break: 退出当前循环体,包括for ,whi ...

  6. 剑指Offer —— BFS 宽度优先打印

    https://www.nowcoder.net/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tqId=11175&tPage= ...

  7. 加入收藏BUG改善

    <script type="text/javascript"> function add_favorite(a, title, url) { url = url || ...

  8. C++之new和malloc差别

         在C++程序猿面试中.非常easy被问到new 和 malloc的差别.偶尔在quora上逛.看到Robert Love的总结.才发现自己仅仅知道里面的一两项就沾沾自喜,从来没有像这位大牛一 ...

  9. IE6\7\8 :last-child 和 :first-chlid 兼容

    IE9以下不支持last-child ,只支持first-child,边框尽量用上边框.

  10. 最大heap

    1 什么是最大heap 最大heap是一棵完全二叉树.每棵子树的根比它的两棵子树上的节点都要大. 2 建堆的过程 function max_heaptify(A): for (i = n/2向下取整: ...