题目:假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列
思路:贪婪
具体:先按身高从高到低排,相同身高根据k从小到大排,再挨个插入
代码:
class Solution(object):
def reconstructQueue(self, people):
"""
:type people: List[List[int]]
:rtype: List[List[int]]
"""
people.sort(key = lambda x: (-x[0], x[1]))
res = []
for p in people:
res.insert(p[1], p)
return res