题目链接:https://leetcode.cn/problems/queue-reconstruction-by-height/
难度:中等

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

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

题解

  1. class Solution:
  2. def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
  3. people.sort(key=lambda x: (x[0], -x[1]), reverse=True)
  4. n = len(people)
  5. ret = []
  6. for i in range(n):
  7. position = people[i][1]
  8. ret.insert(position, people[i])
  9. return ret