Distance to next dedent: hints, no solution

Medium · Stack

A code viewer wants to draw a guide line beside each line of a file that stops where the block ends. You are given the indentation depth of every line, in file order.

For each line, count how many lines further down the file you must go to reach the first line whose indentation is strictly smaller than this line's. Return that count for every line, in order. If no later line is indented less, the answer for that line is 0.

For example, depths 3, 1, 2, 0 give 1, 2, 1, 0.

Examples

Input
indents = [0, 2, 4, 4, 2]
Output
[0, 0, 2, 1, 0]
Note
No line below the first two is shallower. The first 4 reaches the 2 after two lines, the second 4 after one.
Input
indents = [3, 1, 2, 0]
Output
[1, 2, 1, 0]
Note
3 meets 1 after one line, 1 meets 0 after two lines, 2 meets 0 after one line.

Constraints

  • 0 <= indents.length <= 150000
  • 0 <= indents[i] <= 100000

Hint 1: a nudge

For one line, what are you searching for further down the file, and is it always the same kind of line?

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.

See pricing

Sign in

Check my resume first