Queue Reconstruction by Height

Problem Id: 406 Difficulty: Medium


Intuition

Solution


class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        people = people[:]
        people.sort(key=lambda x: x[1])
        people.sort(key=lambda x: x[0], reverse=True)
        ans = []
        for h, k in people:
            ans = ans[:k] + [[h, k]] + ans[k:]
        return ans