当前位置:网站首页>406. reconstruct the queue based on height
406. reconstruct the queue based on height
2022-06-12 17:22:00 【Zzu dish】
406. Rebuild the queue according to height
Let's say there's a group of people in a disordered order standing in a line , Array people Properties that represent some people in the queue ( Not necessarily in order ). Every people[i] = [hi, ki] It means the first one i The height of an individual is hi , front Just right Yes ki Height greater than or equal to hi People who .
Please reconstruct and return the input array people The queue represented by . The returned queue should be formatted as an array queue , among queue[j] = [hj, kj] It's number one in the queue j Personal attributes (queue[0] It's the people at the front of the line ).
Example 1:
Input :people = [[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]]
explain :
The number is 0 The height of a person who is 5 , No one is taller or the same in front of him .
The number is 1 The height of a person who is 7 , No one is taller or the same in front of him .
The number is 2 The height of a person who is 5 , Yes 2 A taller or the same person is in front of him , That is, the number is 0 and 1 People who .
The number is 3 The height of a person who is 6 , Yes 1 A taller or the same person is in front of him , That is, the number is 1 People who .
The number is 4 The height of a person who is 4 , Yes 4 A taller or the same person is in front of him , That is, the number is 0、1、2、3 People who .
The number is 5 The height of a person who is 7 , Yes 1 A taller or the same person is in front of him , That is, the number is 1 People who .
therefore [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] It's the reconstructed queue .
Example 2:
Input :people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output :[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
reflection
We consider People There are two dimensions high( height )、pre( The number of people in front )
We certainly can't think of two dimensions together , So much so that the head and the tail .
Let's first consider high
Yes people Execute collation :
- high The big one is in front
- high identical ,pre The small one is in the front
Before sorting :[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
After sorting : [[7,0], [7,1], [6,1], [5,0], [5,2],[4,4]]
Why do we think so ?
Why is the tall one in front ?
We can iterate over the sorted people Array , Insert operation , Insert at pre.
Because the tall one is in front , Therefore, the subsequent insertion has no effect on the previously inserted ones .
The process of insertion :
- Insert [7,0]:[[7,0]]
- Insert [7,1]:[[7,0],[7,1]]
- Insert [6,1]:[[7,0],[6,1],[7,1]]
- Insert [5,0]:[[5,0],[7,0],[6,1],[7,1]]
- Insert [5,2]:[[5,0],[7,0],[5,2],[6,1],[7,1]]
- Insert [4,4]:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Code implementation
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o2[0]==o1[0]){
return o1[1]-o2[1];
}
return o2[0]-o1[0];
}
});
ArrayList<int[]> list=new ArrayList<>();
for (int i=0;i<people.length;i++){
int postion=people[i][1];
list.add(postion,people[i]);
}
return list.toArray(new int[people.length][people[0].length]);
}
边栏推荐
- Interesting LD_ PRELOAD
- R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置cex.axis参数改变轴标签数值的大小
- The R language uses the aggregate The plot function visualizes the summary statistical information of each subset (visualization is based on the probability value and its 95% confidence interval of th
- How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
- Swintransformer network architecture
- Lambda - 1
- (四)Golang运算符
- qemu+gdb小节
- 迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
- Learn the mitmproxy packet capturing tool from scratch
猜你喜欢
Dongfeng Yueda Kia, Tencent advertising and hero League mobile game professional league cooperate to build a new E-sports ecology
Gerrit+2触发Jenkins任务
邱盛昌:OPPO商业化数据体系建设实战
Learn the mitmproxy packet capturing tool from scratch
php 实现无限极分类树(递归及其优化)
Google browser debugging skills
Interesting LD_ PRELOAD
Picture online collection and delivery system source code
Compilation optimization of performance optimization
Swintransformer network architecture
随机推荐
Volcano engine held a video cloud technology force summit and released a new experience oriented video cloud product matrix
Saturated! Can't future programmers work anymore?
错误记录:IllegalStateException: Optional int parameter ‘xxxx‘ is
How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
Yyds dry goods inventory leetcode question set 911 - 920
2080 virtual machine login command
分辨率与行场同步信号的关系 场消隐
(4) Golang operator
Some minor problems and solutions encountered when using ubantu
写技术博客的意义
STL -- function object
Gerrit触发Jenkins SonarQube扫描
The R language uses the plot function to visualize the data scatter chart, and uses font The axis parameter specifies that the font type of the axis scale label is italic
Evolution and thinking of Taobao native R & D mode | DX R & D mode
Installation and use of rolabelimg
Operating with idle funds
How to change Golan back to the English version when it becomes the Chinese version
Sudo of uabntu
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容
R语言使用plot函数可视化数据散点图,使用font.axis参数指定坐标轴刻度标签的字体类型为斜体字体(italic)