Queue Reconstruction by Height
Put a shuffled list of people back into line, given each person's height and how many people at least as tall stand in front.
People stand in a queue, and each person is described by a pair [h, k]hkhpeople
Example 1
Output: [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
Example 2
Output: [[4, 0], [5, 0], [2, 2], [3, 2], [1, 4], [6, 0]]
The Idea
Imagine a line of people where everyone has a note in their pocket. The note says their height and how many people ahead of them are at least as tall. Then the line breaks up, and only the notes are left. Can we put everyone back in their place?
Check the notes against the answer to Example 1. The person [6, 1][4, 4]
Look at [6, 1]
Start with the tallest. When someone joins, everyone already in the line is at least as tall as them, so all of them count. A person with count kkk
Each row places one more person, and the highlighted box is the new one. In the row labelled "[6, 1] at position 1", the 6 goes in behind [7, 0][7, 1][7, 1]
The two people of height 7 show how to break a tie. They count each other, so [7, 0][7, 1][4, 4]
Each person is put in their final spot the moment they come up, using only the people placed so far, and nobody is moved by hand afterwards. Placing each person straight at the position their count names, tallest first, without trying other orders, is what makes this a greedy solution.
Solution Approach
Turning that idea into code means three things: seeing why trying every order is not needed, choosing the order in which people are placed, and choosing what holds the growing line.
Why not try every order
Could we try every way to arrange the people and check all the counts? Six people can stand in 720 different orders, and twelve people in nearly 480 million. The tallest-first order goes straight to the answer instead, placing each person once.
The order to place people in
Which person goes first? Sort the people by height, tallest first, and break ties by count, smallest first. For Example 1 that gives [7, 0][7, 1][6, 1][5, 0][5, 2][4, 4]
Shortest first does not work. In Example 1, [4, 4]
Holding the growing line
What should hold the line while people join? Each person goes into the middle, so the people behind them must move back one place. A Java ArrayListadd(index, element)indexindexint[]
Java Solution
The method reconstructQueue
- First, sort
withpeople and a comparator. When the heightsArrays.sort anda[0] differ,b[0] puts the taller person first. When they are equal,b[0] - a[0] puts the smaller count first.a[1] - b[1] - Create
, an emptyqueue that holds the line as it grows.ArrayList<int[]> - Loop over the sorted people. Each person
goes in withp , at the position given by their countqueue.add(p[1], p) .p[1] - When the loop is done, turn
back into anqueue withint[][] and return it.toArray(new int[0][])
The main
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class QueueReconstructionByHeight {
public static int[][] reconstructQueue(int[][] people) {
// tallest first; for equal heights, the smaller k first
Arrays.sort(people, (a, b) -> a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]);
List<int[]> queue = new ArrayList<>();
for (int[] p : people) {
queue.add(p[1], p); // everyone already placed is at least as tall
}
return queue.toArray(new int[0][]);
}
public static void main(String[] args) {
int[][][] inputs = {
{{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}},
{{6, 0}, {5, 0}, {4, 0}, {3, 2}, {2, 2}, {1, 4}},
{{5, 2}, {5, 0}, {5, 1}}, // everyone the same height
{{1, 0}, {2, 0}, {3, 0}}, // nobody taller in front of anyone
{{4, 0}},
{}
};
for (int[][] people : inputs) {
System.out.println(Arrays.deepToString(reconstructQueue(people)));
}
}
}
Running it prints:
[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
[[4, 0], [5, 0], [2, 2], [3, 2], [1, 4], [6, 0]]
[[5, 0], [5, 1], [5, 2]]
[[1, 0], [2, 0], [3, 0]]
[[4, 0]]
[]
The line [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]][[5, 0], [5, 1], [5, 2]]
Now look at [[1, 0], [2, 0], [3, 0]][]
The tie rule matters. With equal heights sorted by the larger count first, the pairs [[7, 0], [7, 1]][7, 1]IndexOutOfBoundsException: Index: 1, Size: 0
One side effect: Arrays.sortpeople
Complexity
The work is one sort plus one insert per person, and the inserts cost the most.
- Time: O(n²), because each of the n inserts into
can shift up to n people already in the line, while the sort takes only O(n log n).queue - Space: O(n), because
and the returned array each hold one reference per person, and sorting an array of arrays can use up to O(n) more.queue
A custom sort order that makes a single greedy pass work is also the heart of