FastPrepPipe Removal Turns

Pipe Removal Turns

Wells Fargo logoWells Fargo● HardFULLTIMEOA
Learn

Problem statement

Pipes are arranged from left to right, and lengths[i] is the length of pipe i.

During each turn, mark every pipe whose length is strictly greater than the pipe immediately to its left. Remove all marked pipes simultaneously.

Repeat until a turn marks no pipes. Return the number of turns in which at least one pipe is removed.

Function

countPipeRemovalTurns(lengths: int[]) → int

Examples

Example 1

lengths = [3,6,9,8]return = 2

Turn 1 removes 6 and 9, leaving [3,8]. Turn 2 removes 8, leaving [3].

Example 2

lengths = [9,7,5,3]return = 0

No pipe is longer than the pipe immediately to its left.

Example 3

lengths = [4,3,7,5,6,4,2]return = 3

The successive stable lists are [4,3,5,4,2], [4,3,4,2], and [4,3,2].

Constraints

  • 2 <= lengths.length <= 10^5.
  • 1 <= lengths[i] <= 10^9.

More Wells Fargo problems

See Wells Fargo hiring insights
public int countPipeRemovalTurns(int[] lengths) {
  // write your code here
}
lengths[3,6,9,8]
expected2
Checking account…