Trains the technique from
LeetCode 406Queue Reconstruction by HeightThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A guide lined a walking group up single file for a photo, wrote one record per hiker, then shuffled the records.
hikers[i] = [height, ahead] is one such record: that hiker measures height centimetres, and of the hikers who stood in front of them in the line, exactly ahead measured height centimetres or more. Hikers of the same height therefore count each other.
Rebuild the line. Return a list ordered from the front of the line to the back, where entry j is the [height, ahead] record of the hiker who stood in position j. Exactly one line agrees with all of the records.
Example 1
Walking the returned line from the front: the hiker of 170 has nobody in front, so 0; the first hiker of 162 has the 170 in front, so 1; the second hiker of 162 has the 170 and the other 162 in front, so 2; the hiker of 175 has nobody as tall in front, so 0; the hiker of 168 has the 170 and the 175 in front, so 2. All five counts match their records.
Example 2
All three hikers are the same height, so each counts the ones in front of them. The counts 0, 1 and 2 fix the order from front to back.
Example 3
In the returned line the three hikers of 172 sit at positions 0, 1 and 4, with 0, 1 and 2 hikers as tall as them in front, and the two of 165 sit at positions 2 and 3, with 2 and 3 in front. The hiker of 180 sits last, and nobody in front is as tall, which matches their record of 0.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def rebuild_line(hikers: list[list[int]]) -> list[list[int]]:public int[][] rebuildLine(int[][] hikers)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.