当前位置:网站首页>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;
}
}
边栏推荐
- Zzuli:1044 failure rate
- Analysis of gene family characteristics - chromosome location analysis
- 创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
- Luogu p5194 [usaco05dec]scales s solution
- Zzuli:1046 product of odd numbers
- Tensor 省略号(三个点)切片
- Fundamentals of PHP deserialization
- Find books ()
- 【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
- Zzuli:1040 sum of sequence 1
猜你喜欢
Qt development - scrolling digital selector commonly used in embedded system
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
Detailed explanation of four modes of distributed transaction (Seata)
556. The next larger element III
tonybot 人形机器人 首次开机 0630
puzzle(016.3)千丝万缕
Use of constraintlayout
随机推荐
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
adc128s022 ADC verilog设计实现
Address book sorting
Talking about part of data storage in C language
Analysis of gene family characteristics - chromosome location analysis
7-1 positive integer a+b (15 points)
7-3 count the number of words in a line of text
Luogu p5536 [xr-3] core city solution
556. 下一个更大元素 III : 简单构造模拟题
C language to realize mine sweeping
mmdetection 学习率与batch_size关系
Zzuli:1053 sine function
Zzuli:1048 factorial table
Get permissions dynamically
NOI OPENJUDGE 1.4(15)
tonybot 人形机器人 红外遥控玩法 0630
Zzuli: sum of 1051 square roots
C language fcntl function
零拷贝底层剖析
Sword finger offer 28 Symmetric binary tree