# 406 Queue Reconstruction by Height

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers `(h, k)`, where `h` is the height of the person and `k` is the number of people in front of this person who have a height greater than or equal to `h`. Write an algorithm to reconstruct the queue.

#### Example：

Input:
[[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]]

#### Note：

The number of people is less than 1,100.

## 1. 贪心

#### 实际耗时：36ms

``````public int[][] reconstructQueue(int[][] people) {
if (people == null || people.length == 0 || people[0].length == 0) {
return new int[0][0];
}

// 首先按照身高进行排序，如果身高一样则按照后面的数字大小排序
Arrays.sort(people, (o1, o2) -> {
if (o1[0] == o2[0]) {
return o1[1] - o2[1];
} else {
return o2[0] - o1[0];
}
});

ArrayList<int[]> tmp = new ArrayList<>();
for (int[] person : people) {
}

int[][] res = new int[people.length][2];
int i = 0;
for (int[] k : tmp) {
res[i][0] = k[0];
res[i++][1] = k[1];
}

return res;
}
``````

我一开始想法很简单：显然一开始需要对数组中的第二个，也就是前面有多少人比它高进行排序，然后就死在这里了，这样是做不出来的。因为比这个数高的那些数字，不一定全在它前面。后来去看了答案，觉得特别不错，但是其实要我想还真的想不出来：首先对数组中的第一个排序，按照从大到小的顺序，如果第一个数字相同，则按照第二个数字从小到大的顺序排。然后按照规则把它插入list中就可以得到解。