Skip to content

LC 0406 [M] Queue Reconstruction by Height

Code with Senpai edited this page Jan 19, 2022 · 2 revisions
[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]

[[7, 0], [7, 1], [6, 1], [5, 0], [5, 2], [4, 4]]

[[7, 0]]
[[7, 0], [7, 1]]
[[7, 0], [6, 1], [7, 1]]
[[5, 0], [7, 0], [6, 1], [7, 1]]
[[5, 0], [7, 0], [5, 2], [6, 1], [7, 1]]
[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        people.sort(key=lambda height_front: [-height_front[0], height_front[1]]) # sort by -height (desc) then +front (asc)
        print(people)
        Q = []
        for p in people:
            height, front = p
            Q.insert(front, p)
        return Q
Clone this wiki locally