当前位置:网站首页>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][]);
}
}
边栏推荐
- Oracle-11gR2默认的系统JOB
- opencv4.60版本安装和配置
- F - Jealous Two-二维逆序对
- 从开发转测试:我从零开始,一干就是6年的自动化测试历程
- 7 C控制语句:分支和跳转
- (IROS 2022) 基于事件相机的单目视觉惯性里程计 / Event-based Monocular Visual Inertial Odometry
- 2022年安全员-B证考试模拟100题及答案
- 【杂谈】程序员的发展最需要两点能力
- leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
- Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
猜你喜欢

2022年危险化学品经营单位安全管理人员上岗证题目及答案

Data analysis interview question summary

RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline

公众号简介

【单细胞高级绘图】07.KEGG富集结果展示

DAPP safety summary and typical safety incident analysis

网络层的IP协议
![[592. Fraction addition and subtraction]](/img/3a/1a76a8acd60a6d45eebed612fd3971.png)
[592. Fraction addition and subtraction]

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.

中国地图省>市>级>区>镇>村5级联动下载【2019和2021】
随机推荐
A perfect cross compilation environment records the shell scripts generated by PETA
【JVM】JVM表示浮点数
01-TensorFlow计算模型(一)——计算图
How to obtain the subordinate / annotation information of KEGG channel
完善的交叉编译环境记录 peta 生成的shell 脚本
[592. Fraction addition and subtraction]
【leetcode周赛总结】LeetCode第 83场双周赛(7.23)
2022高压电工考试模拟100题及模拟考试
Setting of parameter configuration tool for wireless vibrating wire collector
Centralized log management with sentry
Kubernetes data persistence scheme
IntelliJ IDEA 关联数据库
DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文
Get started quickly with flask (I) understand the framework flask, project structure and development environment
12 common design ideas of design for failure
Marketing play is changeable, and understanding the rules is the key!
C simply call FMU for simulation calculation
JSON 文件存储
Machine learning (11) -- time series analysis
Sword finger offer