当前位置:网站首页>LeetCode_ 406_ Rebuild the queue based on height
LeetCode_ 406_ Rebuild the queue based on height
2022-07-28 09:20:00 【Fitz1318】
Topic link
Title Description
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]]
Tips :
1 <= people.length <= 20000 <= hi <= 10^60 <= ki < people.length- The topic data ensures that the queue can be rebuilt
Their thinking
The law of greed
- Sort by height , For those with the same height k From small to large
AC Code
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return b[0] - a[0];
});
LinkedList<int[]> ans = new LinkedList<>();
for (int[] p : people) {
ans.add(p[1], p);
}
return ans.toArray(new int[people.length][]);
}
}
边栏推荐
- Sentry log management system installation and use tutorial
- Centralized log management with sentry
- 站在大佬的肩膀上,你可以看的更远
- 19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
- Prometheus TSDB analysis
- 2022 safety officer-b certificate examination simulated 100 questions and answers
- 个人博客小程序
- 信息学奥赛一本通 1617:转圈游戏 | 1875:【13NOIP提高组】转圈游戏 | 洛谷 P1965 [NOIP2013 提高组] 转圈游戏
- Kubernetes cluster configuration DNS Service
- 2022 safety officer-c certificate special operation certificate examination question bank and answers
猜你喜欢

Linux initializes MySQL with fatal error: could not find my-default.cnf

【一花一世界-郑一教授-繁简之道】可解释神经网络

Principle of line of sight tracking and explanation of the paper

Vs2015 use dumpbin to view the exported function symbols of the library

CSV file storage

opencv4.60版本安装和配置

linux初始化mysql时报错 FATAL ERROR: Could not find my-default.cnf

正负数值的正则表达式

AMQ streams (1) of openshift 4 - multiple consumers receive data from partition

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
随机推荐
Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
AMQ streams (1) of openshift 4 - multiple consumers receive data from partition
Learn to draw with nature communications -- complex violin drawing
2022 high voltage electrician examination simulated 100 questions and simulated examination
10. Learn MySQL like clause
[advanced drawing of single cell] 07. Display of KEGG enrichment results
App加速读取显示IPFS图片的解决方案和实现
Force deduction question (1) -- sum of two numbers
Mysql5.7.38 start keepalived in the container
MySQL 8.0.30 GA
opencv4.60版本安装和配置
一款入门神器TensorFlowPlayground
看得清比走得快更重要,因为走得对才能走得远
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared
剑指offer
2022 safety officer-b certificate examination simulated 100 questions and answers
sql server 的关键字在哪张系统表?
DAPP safety summary and typical safety incident analysis
完善的交叉编译环境记录 peta 生成的shell 脚本