当前位置:网站首页>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;
}
}
边栏推荐
- [graphics] efficient target deformation animation based on OpenGL es 3.0
- 2021-10-16 initial programming
- Detailed explanation of four modes of distributed transaction (Seata)
- Talking about part of data storage in C language
- Pyqt interface production (login + jump page)
- The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
- C language dup2 function
- 分布式事务(Seata) 四大模式详解
- [combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)
- 【7.3】146. LRU缓存机制
猜你喜欢
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
分布式事务(Seata) 四大模式详解
Tonybot humanoid robot infrared remote control play 0630
Dllexport et dllimport
puzzle(016.4)多米诺效应
Creation of data table of Doris' learning notes
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
分布式事务(Seata) 四大模式详解
随机推荐
PS tips - draw green earth with a brush
光猫超级账号密码、宽带账号密码 获取
cpu飙升排查方法
Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Mongodb index
NOI OPENJUDGE 1.3(06)
X86 assembly language - Notes from real mode to protected mode
Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
Déformation de la chaîne bm83 de niuke (conversion de cas, inversion de chaîne, remplacement de chaîne)
C language to implement a password manager (under update)
dllexport和dllimport
[opengl] pre bake using computational shaders
Creation of data table of Doris' learning notes
[opengl] bone animation blending effect
puzzle(016.3)千丝万缕
洛谷P3065 [USACO12DEC]First! G 题解
Protobuf and grpc
Luogu p5194 [usaco05dec]scales s solution
Zzuli:1041 sum of sequence 2