Queue Reconstruction by Height

Queue Reconstruction by Height

Sort Insert Version - 84ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def reconstructQueue(self, people):
"""
:type people: List[List[int]]
:rtype: List[List[int]]
"""
if not people:
return []
n = len(people)
people_sorted = sorted(people, key = lambda x: (-x[0],x[1]))
res = [people_sorted[0]]
for i in range(1,n):
hi, ki = people_sorted[i]
res.insert(ki, people_sorted[i])
return res

test code

1
2
3
4
In [161]: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
In [162]: s = Solution(); t = s.reconstructQueue(people); print(t)
[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]

leet code