Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 903 Bytes

406.md

File metadata and controls

32 lines (23 loc) · 903 Bytes

406 Queue Reconstruction by Height

Description

link


Solution

  • Pick out tallest group of people and sort them in a subarray (S). Since there's no other groups of people taller than them, therefore each guy's index will be just as same as his k value.
  • For 2nd tallest group (and the rest), insert each one of them into (S) by k value. So on and so forth.
  • E.g.
    • input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
    • subarray after step 1: [[7,0], [7,1]]
    • subarray after step 2: [[7,0], [6,1], [7,1]]

Code

Complexity T : O((log(n))

class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        queue = []
        people.sort(key = lambda x: [-x[0], x[1]])
        for p in people:
            queue.insert(p[1], p)
        return queue