Sign in

[Java][Greedy][LeetCode] Queue Reconstruction by Height #406

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]]

Because the larger the number, the less of the number larger than him, and the smaller the number, the more of numbers larger than him. Therefore, I sort by people[i][0], from larger to smaller. If there are the same numbers, I accord to number of people[i][1] and sort from smaller to larger.

因為越大的數字,比他大的數字會越少,越小的數字,比他大的數字會越多。因此,我們依people[i][0]大小來將數組排序,越大的數放前面,當遇到同樣的數字時,則依people[i][1]的值由小到大排序,等一下要依照people[i][1]來重新排序數組。

Then, I need to reorder the array and it is according to people[i][1].