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 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]]
Runtime: 32 ms, faster than 52.69% of C++ online submissions for Queue Reconstruction by Height.
按身高降序排列,身高相同的按第二个数升序排列。
然后我们一个一个按照第二个数的index插入一个新的序列。这样做的好处是,插入后面的数不会对之前的数产生
影响。
但这种做法需要新开一个数组,如果你不想新开一个数组,可以在当前数组种修改,但如果是vector其实提升效果一般吧,最好开一个链表。
#include <assert.h>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
#define ALL(x) (x).begin(), (x).end()
using namespace std;
class Solution {
public:
static bool sortcmp(const pair<int,int> a, const 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>> ret;
if(people.empty()) return ret;
sort(ALL(people), sortcmp);
for(int i=; i<people.size(); i++){
ret.insert(ret.begin() + people[i].second, people[i]);
}
return ret;
}
};
LC 406. Queue Reconstruction by Height的更多相关文章
- LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...
- 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 ...
- 406. Queue Reconstruction by Height
一开始backtrack,设计了很多剪枝情况,还是TLE了 ..后来用PQ做的. 其实上面DFS做到一半的时候意识到应该用PQ做,但是不确定会不会TLE,就继续了,然后果然TLE了.. PQ的做法和剪 ...
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 406. Queue Reconstruction by Height
https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- [leetcode] 406. Queue Reconstruction by Height (medium)
原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
随机推荐
- shell脚本获取传递的参数
1 脚本编写 #!/bin/bash 2 解释 $n 表示是第几个参数 $0 表示脚本命令本身 3 执行效果
- 超简单!教你如何修改源列表(sources.list)来提高软件访问速度
因为Ubuntu官方的源地址不在国内,所以在国内的访问速度非常慢,比如:我们要下载或是更新软件那速度比蜗牛还慢.所以,我们需要改成国内的镜像服务器,这样,我们在下载或更新软件的时候就会很快了. 配置步 ...
- Linux ping route nslookup ifconfig arp traceroute
route -n 查看默认网关 ping -c 包个数 ping -s 包大小 host 目标主机 主机解析 nslookup 目标主机 arp -an 查看arp arp -s IP地 MA ...
- POM标签大全详解
父(Super) POM <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "htt ...
- Hive的视图和索引(九)
Hive的视图和索引 1.Hive Lateral View 1.基本介绍 Lateral View用于和UDTF函数(explode.split)结合来使用. 首先通过UDTF函数拆分成多行 ...
- 【XDOJ】坑爹的杜神
原题: 众所周知,杜神非常喜欢出大模拟,也非常喜欢设置一些细节坑人.例如,在某次大赛中,他出了一道这样的题 (以下省略3000字) 计算出答案a后,你应该将a除以1000,再保留到小数点后两位输出,四 ...
- err:LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLE
err:LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLE err:LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLE err:LIBUSB_S ...
- MyBatis---join 查询
在实际业务中,经常能碰到多表关联查询 下面的Demo,讲举例join查询在MyBatis中的实现 User 类: package com.zy.domain; import java.io.Seria ...
- 使用js拆分带参数的URL,将参数分离出来
url中的内容www.XXXX.com?content=123; 一下为js内容,包装在一个init方法中. init(); function init(){ var theRequest = new ...
- 详解 @MapperScan 注解和 @Mapper 注解
实际上,这是一个非常简单的问题.我并没有一口回绝他,让他去百度.因为,新人都会经历这个过程.好不容易,问你一次,你直接让他百度,会打击到他的.而且,别人会觉得你摆架子. @Mapper 这个注解的定义 ...