Outline numbering: hints, no solution
Medium · Stack
A document tool stores an outline as one indentation depth per line, top to bottom. The first line has depth 0 and no line is more than one level deeper than the line before it, though it may be any number of levels shallower.
Number the lines the way a printed outline does. Top-level lines are numbered 1, 2, 3 and so on. A line at depth 1 is numbered after its nearest shallower line, so the second child of the third top-level line is 3.2. Counting restarts at 1 whenever a new parent begins.
Return the label of every line, in order, as strings with parts joined by a dot. For depths 0, 0, 1, 1, 2, 0 the labels are 1, 2, 2.1, 2.2, 2.2.1, 3.
Examples
- Input
- depths = [0, 0, 1, 1, 2, 0]
- Output
- ["1", "2", "2.1", "2.2", "2.2.1", "3"]
- Note
- The two children of the second top-level line are 2.1 and 2.2, and 2.2 has one child.
- Input
- depths = [0, 1, 2, 1, 0, 1]
- Output
- ["1", "1.1", "1.1.1", "1.2", "2", "2.1"]
- Note
- After 1.1.1 the depth returns to 1, so counting continues with 1.2, then the top level moves on to 2.
Constraints
- 1 <= depths.length <= 450
- depths[0] = 0
- 0 <= depths[i] <= depths[i-1] + 1
Hint 1: a nudge
Each label is a path. What has to be true about the labels of the lines above it when you reach a line of a given depth?
Unlock the full hints and timed practice with a pass
The second and third hints and the timed, runner-checked practice are part of a pass.