You are given an array of people, people
, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki]
represents the ith
person of height hi
with exactly ki
other people in front who have a height greater than or equal to hi
.
Reconstruct and return the queue that is represented by the input array people
. The returned queue should be formatted as an array queue
, where queue[j] = [hj, kj]
is the attributes of the jth
person in the queue (queue[0]
is the person at the front of the queue).
Example 1:
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.
Example 2:
Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]] Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
Constraints:
1 <= people.length <= 2000
0 <= hi <= 106
0 <= ki < people.length
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to reconstructing the queue involves trying every possible arrangement of people. We systematically place each person one at a time, checking if their placement satisfies the given conditions. If it does, we continue, otherwise, we backtrack and try a different arrangement.
Here's how the algorithm would work step-by-step:
def reconstruct_queue_brute_force(people):
number_of_people = len(people)
reconstructed_queue = []
result = []
def solve(index):
nonlocal result
if index == number_of_people:
result = reconstructed_queue[:]
return True
person_height = people[index][0]
people_in_front = people[index][1]
for position in range(len(reconstructed_queue) + 1):
reconstructed_queue.insert(position, people[index])
# Validate the reconstructed queue at this index
taller_or_equal = 0
for i in range(position):
if reconstructed_queue[i][0] >= person_height:
taller_or_equal += 1
# Backtrack if the current arrangement is invalid.
if taller_or_equal != people_in_front:
reconstructed_queue.pop(position)
continue
# Recursively try placing the next person.
if solve(index + 1):
return True
# Backtrack after recursive call returns
reconstructed_queue.pop(position)
return False
solve(0)
return result
The key to solving this problem efficiently is to think about the people from tallest to shortest. By placing taller people first, we can easily figure out where the shorter people should go without messing up the taller people's positions.
Here's how the algorithm would work step-by-step:
def reconstruct_queue(people):
# Sort people by height (descending) and k value (ascending).
people.sort(key=lambda x: (-x[0], x[1]))
reconstructed_queue = []
# Insert each person at the index specified by their k value.
for person in people:
index_to_insert = person[1]
# Inserting at the correct index is crucial.
reconstructed_queue.insert(index_to_insert, person)
# Return the reconstructed queue.
return reconstructed_queue
Case | How to Handle |
---|---|
Null or empty input array | Return an empty list to signify no reconstruction possible. |
Input array with one element | Return the input array as is, as it is already trivially reconstructed. |
All people have the same height and k values are sorted ascending | The algorithm must insert them sequentially at indices 0, 1, 2, etc. |
All people have the same height and k values are sorted descending | The algorithm must insert them sequentially at indices k, k, k, etc. which might cause issues with re-insertion. |
People with the same height but different k values | Sort by height descending, then k ascending to resolve conflicts during insertion. |
Large input array (performance implications) | Choose an efficient sorting algorithm (e.g., merge sort or quicksort) for the initial sort to avoid quadratic time complexity. |
k value is out of bounds (k < 0 or k >= n) | Throw an IllegalArgumentException or return null as this is an invalid input. |
Integer overflow when calculating indices (if using cumulative sums or similar) | Use long data type to avoid overflow when summing or calculating indices. |