The terracotta row
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- lists
- loops
- indexes
Problem
Mei, an archaeologist in Xi'an, lifted a whole row of terracotta warriors out of the pit to clean them, and in the workshop the figures got mixed up. What saves her is the label she stuck on each one as it came out:
how many figures taller than it stood in front of it in the row.
You get heights, a list of positive whole numbers, all different (it may be empty), and ahead, of the same length: ahead[i] is how many figures taller than heights[i] stood before it. The data always comes from a real row, and since the heights differ, only one row matches every label. Return the heights in the order of that row, from front to back. With no figures, return an empty list.
With [5, 3, 7, 6] and [1, 2, 0, 0] you return [6, 5, 3, 7]. Check: nobody is in front of the 6; in front of the 5 is the 6, one taller; in front of the 3 are the 6 and the 5, two taller; in front of the 7 are the 6, the 5 and the 3, but none is taller. All four labels match.
Examples
The example
[5, 3, 7, 6], [1, 2, 0, 0] → [6, 5, 3, 7]
Seven warriors
[7, 4, 1, 2, 8, 9, 5], [1, 0, 6, 2, 1, 0, 2] → [4, 9, 2, 7, 5, 8, 1]
Nobody had a taller one ahead
[8, 4, 6, 2], [0, 0, 0, 0] → [2, 4, 6, 8]
From tallest to shortest
[6, 2, 8, 4], [1, 3, 0, 2] → [8, 6, 4, 2]
A single warrior
[12], [0] → [12]
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def build_row(heights, ahead):
passJavaScript
function buildRow(heights, ahead) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.