当前位置:网站首页>406. 根据身高重建队列
406. 根据身高重建队列
2022-07-03 14:47:00 【酱酱熊】
题目链接
分析:题目意思是要我们将一个数组 people[i] = {h,k},放到一个位置,并且能保证,这个位置前面有k个h大于等于当前h的。
首先说一下需要使用到的变量:
res[][]:用来记录最终答案的二维数组
vis[]:用来记录这个位置是否已经放置了元素
count:用来记录前面有几个大于等于当前h的元素
那么我们可以反过来想,首先找到一个最小的h,那么其它的h肯定比当前大,那么只需要考虑前面有k个位置就行,也就是说,当前这个放到第k+1个位置就行。(这里为了简单,先没考虑有相同的h)
下面我们再考虑有相同h的情况。
其实也很好考虑。
我们从res的第0个开始遍历,当count还小于k的时候,那么说明还要继续往后遍历,因为还没找到合适的位置。
那么什么时候count自增呢,也就是说,什么时候才叫找到了一个大于等于当前h的位置呢?
第一:遍历到当前位置没有被访问,因为每次我们都需要拿出people中剩下的最小的,所以,还没有被访问的肯定是放后面元素的,所以肯定是大于等于当前h的,此时count+1.
第二:遍历到元素等于当前h,也就是有相同h存在的情况,此时也是count+1。
直到People都放到了res即可。
那么此时还有两个问题
问题一:就是我们每次怎么去找h最小的?
问题二:并且如果当前剩下的people中最小的h有多个,应该选择哪一个?
问题一:
我们可以将people放到小顶堆中,这样每次拿到的就是h最小的。
问题二:
如果h相同,那么我们应该选择k最小的,因为h相同,k最小的肯定放在前面。
代码:
class Solution {
public int[][] reconstructQueue(int[][] people) {
//小顶堆 h小的放上面, h相同则k小的放上面
Queue<int[]> queue = new PriorityQueue<>((i1,i2)->{
if(i1[0]!=i2[0]){
return i1[0]-i2[0];
}else{
return i1[1]-i2[1];
}
});
int n = people.length;
int[][] res = new int[n][2];
//记录这个位置是否被访问过
boolean[] vis = new boolean[n];
for(int i=0; i<n; i++){
queue.offer(people[i]);
}
while(!queue.isEmpty()){
int[] top = queue.poll();
int count=0;
int i=0;
while(i<n && count<top[1]){
//这个位置没被访问过 或者 这个位置访问过了,但是h相等
if(!vis[i] || res[i][0]==top[0]){
count++;
}
i++;
}
while(vis[i]){
i++;
}
res[i] = top;
vis[i] = true;
}
return res;
}
}
边栏推荐
- C language to implement a password manager (under update)
- dllexport和dllimport
- 分布式事务(Seata) 四大模式详解
- Implement Gobang with C language
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
- Sword finger offer 28 Symmetric binary tree
- Zzuli:1057 prime number determination
- Find books ()
- 7-3 rental (20 points)
- Niuke bm83 string deformation (case conversion, string inversion, string replacement)
猜你喜欢
Use of form text box (I) select text
dllexport和dllimport
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
链表有环,快慢指针走3步可以吗
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
cpu飙升排查方法
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
How to query the baby category of tmall on Taobao
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
分布式事务(Seata) 四大模式详解
随机推荐
Implement Gobang with C language
Yolov5进阶之九 目标追踪实例1
ConstraintLayout 的使用
Common commands for getting started with mongodb database
链表有环,快慢指针走3步可以吗
Qt development - scrolling digital selector commonly used in embedded system
Find books ()
NOI OPENJUDGE 1.4(15)
Fundamentals of PHP deserialization
Some concepts about agile
Protobuf and grpc
Code writing and playing method of tonybot humanoid robot at fixed distance
Common shortcut keys in PCB
7-10 stack of hats (25 points) (C language solution)
基因家族特征分析 - 染色体定位分析
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Talking about part of data storage in C language
7-3 rental (20 points)
Zzuli:1058 solving inequalities