[LeetCode] 406. Queue Reconstruction by Height

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue).

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]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is 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]] 

Constraints:

  • 1 <= people.length <= 2000
  • 0 <= hi <= 106
  • 0 <= ki < people.length
  • It is guaranteed that the queue can be reconstructed.

根据身高重建队列。

假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。

请你重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/queue-reconstruction-by-height
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给一个二维数组[h, k],其中h表示每个人的身高,k表示在这个人之前有几个人的身高大于等于这个人。input的顺序是错的,请你根据每个人的[h, k]的状况对input进行正确排序。

思路是先按照h从大到小排序,如果h相同,则k小的在前。排序过后是类似这样。

此时创建一个list,将排序后的结果加入list,加入的规则是将每个人的[h, k]加入到list的k位置上。

时间O(nlogn)

空间O(n^2) - output

Java实现

 1 class Solution {
 2     public int[][] reconstructQueue(int[][] people) {
 3         Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
 4         // 新建一个List
 5         List<int[]> list = new ArrayList<>();
 6         // 将每个数组中第二个数值作为下标,将该数组插入List
 7         for (int[] p : people) {
 8             list.add(p[1], p);
 9         }
10         // 将list转为数组返回
11         int[][] res = new int[people.length][2];
12         for (int i = 0; i < list.size(); i++) {
13             res[i] = list.get(i);
14         }
15         return res;
16     }
17 }

 

JavaScript实现

 1 /**
 2  * @param {number[][]} people
 3  * @return {number[][]}
 4  */
 5 var reconstructQueue = function (people) {
 6     let res = [];
 7     people.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
 8     people.forEach((val) => {
 9         res.splice(val[1], 0, val);
10     });
11     return res;
12 };

 

LeetCode 题目总结

posted @ 2020-06-07 02:24  CNoodle  阅读(96)  评论(0编辑  收藏  举报