Program to recover shuffled queue of people in python

Suppose we have a 2D matrix where each row contains two values [height, count] that indicates a person has given height and there are 'count' number of people in front of them that are at least as tall as them. When this queue is shuffled, we need to recover the original ordering.

Problem Understanding

Given a shuffled queue representation, we need to reconstruct the original queue order. Each person is represented as [height, count] where count is the number of people in front who are taller or equal in height.

Example Input and Output

Input (Shuffled)
Height Count
2 2
4 0
5 0
Output (Original Order)
Height Count
4 0
5 0
2 2

Algorithm Steps

To solve this problem, we follow these steps ?

  • Sort matrix rows by increasing height and decreasing count
  • Create an answer array of size N with all entries initially null
  • For each person [height, count], find the correct position by counting available spots
  • Place the person at the position where exactly 'count' taller/equal people are in front

Implementation

class Solution:
    def solve(self, matrix):
        N = len(matrix)
        # Sort by height ascending, then by count descending
        matrix.sort(key=lambda x: [x[0], -x[1]])
        ans = [None] * N

        for h, c in matrix:
            temp = 0
            for i, num in enumerate(ans):
                # If we've found enough spots and current position is empty
                if temp >= c and num is None:
                    ans[i] = [h, c]
                    break
                
                # Count empty spots or people taller/equal in height
                if num is None or num[0] >= h:
                    temp += 1
        
        return ans

# Test the solution
ob = Solution()
matrix = [
    [2, 2],
    [4, 0],
    [5, 0]
]
result = ob.solve(matrix)
print("Original queue order:")
for person in result:
    print(f"Height: {person[0]}, Count: {person[1]}")
Original queue order:
Height: 4, Count: 0
Height: 5, Count: 0
Height: 2, Count: 2

How It Works

The algorithm works by processing people in a specific order. We first sort by height (ascending) and then by count (descending). This ensures that when we place a person, all the people we've already placed are either shorter or have a higher count value, which helps maintain the constraint.

For the person with height 2 and count 2, we need exactly 2 people in front who are at least as tall. The algorithm finds this position by counting available spots until it reaches the required count.

Queue Recovery Process 1. Sort by height: [2,2] [4,0] [5,0] 2. Place in order: [4,0] [5,0] [2,2] Person [2,2] needs 2 people ? height 2 in front Both [4,0] and [5,0] satisfy this condition

Conclusion

This algorithm efficiently reconstructs the original queue by sorting the people strategically and placing them in positions that satisfy their count constraints. The key insight is processing people by height order to ensure correct placement.

Updated on: 2026-03-25T12:47:49+05:30

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements