当前位置:网站首页>406. Reconstruct the queue according to height
406. Reconstruct the queue according to height
2022-07-03 14:51:00 【Sauce bear】
Topic link
analysis : The title means that we should put an array people[i] = {h,k}, Put it in one place , And can guarantee , There is k individual h Greater than or equal to the current h Of .
First, let's talk about the variables that need to be used :
res[][]: A two-dimensional array used to record the final answer
vis[]: It is used to record whether the element has been placed in this position
count: It is used to record the number of previous ones greater than or equal to the current h The elements of
Then we can think the other way around , First find the smallest h, So the others h It must be bigger than now , Then we only need to consider the following k Just one position , in other words , Currently, this is placed in the k+1 Just one position .( And here for simplicity , I didn't consider having the same h)
Now let's consider the same h The situation of .
In fact, it's also very easy to consider .
We from res Of the 0 Start traversing , When count Less than k When , Then it means that we need to continue to traverse later , Because I haven't found the right place yet .
So when count Self increasing , in other words , When can I find a greater than or equal to the current h The location of ?
First of all : Traverse to the current location without being accessed , Because every time we need to take out people The smallest remaining , therefore , Those that have not been accessed must be placed behind the elements , So it must be greater than or equal to the current h Of , here count+1.
second : Traverse to the element equal to the current h, That is, there are the same h Existing situation , At this time, too count+1.
until People It's all there res that will do .
Then there are two more questions
Question 1 : It's how we find it every time h The smallest ?
Question two : And if the current remaining people The smallest of all h There are many. , Which one to choose ?
Question 1 :
We can people Put it in the small top pile , So every time you get h The smallest .
Question two :
If h identical , So we should choose k The smallest , because h identical ,k The smallest must be in front .
Code :
class Solution {
public int[][] reconstructQueue(int[][] people) {
// Small cap pile h Put the small one on it , h The same thing k Put the small one on it
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];
// Record whether this location has been visited
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]){
// This location has not been visited perhaps This location has been visited , however h equal
if(!vis[i] || res[i][0]==top[0]){
count++;
}
i++;
}
while(vis[i]){
i++;
}
res[i] = top;
vis[i] = true;
}
return res;
}
}
边栏推荐
- Niuke: crossing the river
- Talking about part of data storage in C language
- 牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
- Bucket sorting in C language
- [ue4] HISM large scale vegetation rendering solution
- Simulation of LS -al command in C language
- C string format (decimal point retention / decimal conversion, etc.)
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- Puzzle (016.4) domino effect
- Address book sorting
猜你喜欢

Tonybot humanoid robot starts for the first time 0630

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
![[ue4] cascading shadow CSM](/img/83/f4dfda3bd5ba0172676c450ba7693b.jpg)
[ue4] cascading shadow CSM

Detailed explanation of four modes of distributed transaction (Seata)

Bucket sorting in C language

Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text

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

5-1 blocking / non blocking, synchronous / asynchronous
![[graphics] efficient target deformation animation based on OpenGL es 3.0](/img/53/852ac569c930bc419846ac209c8d47.jpg)
[graphics] efficient target deformation animation based on OpenGL es 3.0

Puzzle (016.4) domino effect
随机推荐
Fundamentals of PHP deserialization
7-9 one way in, two ways out (25 points)
Zzuli: sum of 1051 square roots
Zzuli:1055 rabbit reproduction
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
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
cpu飙升排查方法
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
To improve efficiency or increase costs, how should developers understand pair programming?
Use of constraintlayout
Bucket sorting in C language
tonybot 人形机器人 首次开机 0630
Dllexport et dllimport
Table of mathematical constants by q779
Devaxpress: range selection control rangecontrol uses
Qt development - scrolling digital selector commonly used in embedded system
Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
Implement Gobang with C language
零拷贝底层剖析
tonybot 人形机器人 查看端口并对应端口 0701