原题

思路:

一开始完全没有思路。。看了别人的思路才解出来。

先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序。

  排完序后我们可以注意到这样一个事实:如果我们按照先处理身最高的,那他们的k值就是他们所应该在的位置——因为已经没有比他们更高的了。

  所以我们从高度从高到低按照k值的位置一直插入到答案中即可。


class Solution {
public:
static bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>> &people) {
vector<pair<int, int>> res;
sort(people.begin(), people.end(), cmp);
for (auto i : people) res.insert(res.begin() + i.second, i);
return res;
}
};

[leetcode] 406. Queue Reconstruction by Height (medium)的更多相关文章

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

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

  2. LN : leetcode 406 Queue Reconstruction by Height

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

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

  4. [leetcode] 406. Queue Reconstruction by Height

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

  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. [Erlang-0015][Lager] Erlang日志框架Lager简析

    项目地址:https://github.com/basho/lager (欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/liangjingyang)

  2. Linux下的wfopen(手工打造)

    Of Linux on wfopen (open wide-character version of the file name and mode) to achieve Not directly a ...

  3. 开启Qt Lite Project

    发布: http://blog.qt.io/blog/2017/01/23/qt-5-8-released/ 说法一:Qt Lite” is not a tool, but a concept tha ...

  4. _stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l, _mbsicmp_l 比较函数

    https://msdn.microsoft.com/en-us/library/k59z8dwe.aspx

  5. 利用apache搭建本地环境

    登陆http://httpd.apache.org/download.cgi到apache的官方下载页面. 选择一个版本,以最新版为例,点击Binaries.

  6. js中prototype与__proto__区别

    proto(隐式原型)与prototype(显式原型) 显式原型 explicit prototype property:每一个函数在创建之后都会拥有一个名为prototype的属性,这个属性指向函数 ...

  7. 接口测试中读取excel中的请求数据含有中文问题,UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position

    错误信息:UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position 31: Body ('小') ...

  8. Spring 5.x 、Spring Boot 2.x 、Spring Cloud 与常用技术栈整合

    项目 GitHub 地址:https://github.com/heibaiying/spring-samples-for-all 版本说明: Spring: 5.1.3.RELEASE Spring ...

  9. Docker配置容器位置和小技巧

    Docker使用小技巧 清理全部停止的docker容器 有时候我们会有很多已经停止的容器或者由于错误强制退出不能用的容器,那我们就需要删除了,但是我们一个一个的rm删除很麻烦,有多少容器就要rm多少次 ...

  10. 浅入深出Vue:事件处理

    上一篇的最后留下了一个 v-on的思考,也就是本章的主题:事件处理 为什么需要事件处理 在前端开发中,经常要面对各种表单.按钮.而这里面就住着一个事件:点击 (click). 前端童鞋们肯定不陌生它, ...