FastPrepRecover Document Hierarchy from Indentation
Problem · Array

Recover Document Hierarchy from Indentation

Learn this problem
EasyReducto logoReductoFULLTIMEPHONE SCREEN

Problem statement

A document parser returns text objects in reading order. texts[i] is the object's text, and distances[i] is [left, top, right, bottom], its distances from the four page borders.

The left distance encodes indentation. A positive baseUnit is one indentation level, and every quotient distances[i][0] / baseUnit is within 0.01 of one unique nonnegative integer.

Return one string per object, preserving reading order, in the form level|text. The level is that unique integer. The other three border distances do not affect it.

Function

recoverIndentation(texts: String[], distances: double[][], baseUnit: double) → String[]

Examples

Example 1

texts = ["Title","Section","Paragraph","Note"]distances = [[0,0,8,1],[2.01,1,5.99,1],[4,2,4,1],[6.02,3,1.98,1]]baseUnit = 2return = ["0|Title","1|Section","2|Paragraph","3|Note"]

Each left distance is within one hundredth of a multiple of 2, so the levels are 0 through 3.

Example 2

texts = ["Heading","Indented","Back"]distances = [[0,7,9,2],[2.49,0,1,3],[1.26,8,4,0]]baseUnit = 1.25return = ["0|Heading","2|Indented","1|Back"]

The levels follow left distance only and preserve input order; they do not need to be monotone.

Constraints

  • 1 <= texts.length == distances.length <= 100000.
  • Each text has length from 1 through 100.
  • Every distance row is [left, top, right, bottom].
  • All distances are finite values from 0 through 1000000 with at most two decimal places.
  • 0.01 <= baseUnit <= 1000000 and it has at most two decimal places.
  • Every left / baseUnit quotient is within 0.01 of one unique nonnegative integer no greater than 1000000.
drafts saved locally
public String[] recoverIndentation(String[] texts, double[][] distances, double baseUnit) {
    // Write your solution here.
}
texts["Title","Section","Paragraph","Note"]
distances[[0,0,8,1],[2.01,1,5.99,1],[4,2,4,1],[6.02,3,1.98,1]]
baseUnit2
expected["0|Title", "1|Section", "2|Paragraph", "3|Note"]
checking account