406. Queue Reconstruction by Height

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (o1, o2) -> {
if (o1[0] == o2[0]) {
// 身高相同时根据个数从低到高排序
return o1[1] - o2[1];
} else {
// 身高不同时根据身高从高到低排序
return o2[0] - o1[0];
}
});

List<int[]> queue = new ArrayList<>();
for (int[] person : people) {
// 前面的人身高都大于等于当前这个人
queue.add(person[1], person);
}

return queue.toArray(new int[0][]);
}
}

References

406. Queue Reconstruction by Height