巧妙的一道题。
思路:
先把所有people按照个头高低排队,在此基础上再安第二个参数由小到大。
然后遍历上述排序后队伍,先把个子最高的都放进去,然后再继续把小的往里插。插的位置就是第二个参数。
python排序用法:
aftersort = sort(list,key = lambda x:(-x[0], x[1]))
python insert用法:
list.insert(index, item)
代码:
class Solution:
def reconstructQueue(self, people):
"""
:type people: List[List[int]]
:rtype: List[List[int]]
"""
if not people:
return []
res = []
sort_people = sorted(people, key=lambda x: (-x[0], x[1]))
max_height = sort_people[0][0]
for people in sort_people:
if people[0] == max_height:
res.append(people)
else:
res.insert(people[1], people)
return res