Problem Overview
You are given an array of people represented as pairs of integers, where each pair is in the form (h, k):
hrepresents the height of a person.krepresents the number of people in front of this person who have a height greater than or equal toh.
Your task is to reconstruct the original queue that satisfies all the given conditions.
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]]
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]]
Intuition / Approach
This problem seems tricky at first because both height and k are interdependent.
However, with a clever sorting and insertion strategy, we can reconstruct the queue step by step.
Key Insight:
- Sort the people first so that taller people are placed before shorter ones.
- For people with the same height, sort them by their
kvalue in ascending order. - This ensures that when we insert people into the queue, shorter people do not affect the order of taller ones.
- For people with the same height, sort them by their
- Insert each person into the queue based on their
kvalue.- The
kvalue represents the position at which this person should appear in the queue because exactlyktaller (or equal height) people must be in front of them.
- The
This approach ensures that every time we insert someone, the condition for their k value automatically holds true, since all previously inserted people are taller or equal in height.
Algorithm Explanation
- Sort the input array:
- Sort primarily by height in descending order (
hfrom high to low). - If two people have the same height, sort by
kin ascending order.
- Sort primarily by height in descending order (
- Insert people one by one:
- Initialize an empty
Listto represent the queue. - For each person in the sorted list:
- Insert them at the position specified by their
kvalue. - Since the list automatically shifts elements to the right, the relative ordering is preserved.
- Insert them at the position specified by their
- Initialize an empty
- Convert the list back to an array and return it.
Java Code Implementation
import java.util.*;
public class QueueReconstructionByHeight {
public int[][] reconstructQueue(int[][] people) {
// Step 1: Sort the array
Arrays.sort(people, (a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1]; // sort by k ascending if height is same
} else {
return b[0] - a[0]; // sort by height descending
}
});
// Step 2: Use a list to insert people based on k value
List<int[]> result = new ArrayList<>();
for (int[] person : people) {
result.add(person[1], person); // insert at index = k
}
// Step 3: Convert list back to 2D array
return result.toArray(new int[people.length][2]);
}
// Example usage
public static void main(String[] args) {
QueueReconstructionByHeight solution = new QueueReconstructionByHeight();
int[][] people = {{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}};
int[][] result = solution.reconstructQueue(people);
System.out.println("Reconstructed Queue:");
for (int[] person : result) {
System.out.println(Arrays.toString(person));
}
}
}
Dry Run Example
Let’s take the input:[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
- After Sorting:
[[7,0],[7,1],[6,1],[5,0],[5,2],[4,4]] - Insert one by one:
- Insert [7,0] → [[7,0]]
- Insert [7,1] → [[7,0], [7,1]]
- Insert [6,1] → [[7,0], [6,1], [7,1]]
- Insert [5,0] → [[5,0], [7,0], [6,1], [7,1]]
- Insert [5,2] → [[5,0], [7,0], [5,2], [6,1], [7,1]]
- Insert [4,4] → [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
Final Output:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Complexity Analysis
Time Complexity:
- Sorting takes O(n log n).
- Insertion into the list for each person takes O(n) in the worst case.
- Therefore, total time complexity = O(n²).
Space Complexity:
- We use an additional list to build the result.
- Space complexity = O(n).
Alternative Approaches
- Using LinkedList for insertion:
- A
LinkedListcould be used for insertion to avoid shifting elements. - However, since
add(index, element)still requires traversal to the index position, time complexity remains O(n²).
- A
- Using a Segment Tree (Advanced):
- In theory, one could use a segment tree or binary indexed tree to improve the insertion logic, but that adds unnecessary complexity.
- For this problem size, the sorting + list insertion approach is optimal and clear.
