406. Queue Reconstruction by Height
Medium
Problem:
There is a queue of people, each represented by a pair of integers (h, k)
, where h
is the person's height, and k
is the number of people in front of this person who have a height greater or equal to h
. Write an algorithm to rearrange the queue such that these values are accurate.
Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
What to learn:
Tuple vs List
The first example uses a tuple to store the person's data:
codeheapq.heappush(heap, (-person[0], person[1]))
The second example uses a list to store the person's data:
codeheapq.heappush(heap, [-person[0], person[1]])
Here's the key distinction between the two:
Tuple: Tuples are immutable, meaning their values cannot be changed after they are created. So, when a person's data is stored as a tuple, it's "locked in" and cannot be altered unless a new tuple is created. Tuples can be used as dictionary keys or inserted into sets because of this immutability.
List: Lists are mutable, meaning their values can be changed after they are created. When a person's data is stored as a list, it can be altered directly.
For the specific code and problem you've presented, the practical effect is minimal since the stored items in the heap are not modified after they are added. But in other scenarios, the choice between tuple and list can be more consequential.
As a general rule of thumb:
Use tuples when you want an ordered collection of items that should not be changed (immutable).
Use lists when you want an ordered collection of items that you might need to modify (mutable).
In this particular context, using tuples might be slightly more memory-efficient and conceptually clearer, since there's no intention to modify the person's data once it's in the heap.
Solution:
The solution can be implemented in the form of a max-heap, where the first value is extracted in descending order, and the second value can be used as the insertion index. Since Python only supports min-heaps, the first value can be negated to implement a min-heap as if it were a max-heap.
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
heap = []
for person in people:
heapq.heappush(heap, (-person[0], person[1]))
result = []
while heap:
person = heapq.heappop(heap)
result.insert(person[1], [-person[0], person[1]])
return result
Last updated