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.
Note:
The number of people is less than 1,100.
Example:
1 | Input: |
这道题拿到的时候就想着要先按高矮进行排序,然后再交换,后来交换的时候绕晕了,看了一眼题解。
基本思路还是先按高矮进行排序,然后用贪心算法。每次取还没有安排位置最矮的。然后从0号位数到应该安排的位置(前边是空位,或者是和当前人一样高的,在当前人之前不可能有比他高的被安排。)
1 | class Solution { |