Rounds of Pruning in a Garden Row
Stacks & QueuesAuto-graded · 21 test casesLast updated
Count how many rounds of simultaneous removals occur in a row of numbers where each element larger than its left neighbor is removed, using a monotonic stack to beat quadratic simulation.
A gardener has a row of shrubs, and each shrub carries an integer toxin reading. The shrubs are listed from left to right in an array.
Process
The row is pruned in rounds:
- In a round, every shrub whose reading is strictly larger than the reading of the shrub directly to its left (in the row as it stands at the start of that round) is marked for removal.
- All marked shrubs are removed at the same moment, at the end of the round.
- The first shrub in the row has no left neighbor, so it is never removed.
- Rounds continue until a round in which no shrub is removed.
Task
Given the array readings, return the number of rounds in which at least one shrub is removed. In other words, return the number of days after which the row no longer changes (the final round with no removals is not counted).
Shrubs with equal readings do not remove each other, since the comparison is strict.
Input
readings: a list of integers, the toxin readings from left to right.
Output
- An integer: the number of rounds in which at least one shrub is removed.
Constraints
1 <= len(readings) <= 10^50 <= readings[i] <= 10^9- A solution that is faster than O(n^2) is expected.
Examples
Example 1
Input: pruning_rounds([6, 5, 8, 4, 7, 10, 9])
Output: 2
Hints
Hint 1
Simulating round by round can take about n rounds of O(n) work each. Think about computing, for each shrub, the round in which it is removed, then taking the maximum.
Hint 2
For a shrub, look at the nearest shrub on its left with a strictly smaller reading. If none exists, it is never removed. Otherwise it can only be removed after every shrub between them is gone.
Hint 3
Use a monotonic stack storing (reading, removal round). Pop entries with reading >= the current one while tracking the largest removal round among them; if the stack is still non-empty, the current removal round is that maximum plus one, else 0.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Simulating every round costs O(n) per round, and there can be about n rounds, so O(n^2) in the worst case. Instead, compute the round in which each shrub is removed (0 meaning never) and return the maximum. That maximum equals the number of rounds with at least one removal, because removal rounds form a contiguous range starting at 1: a shrub removed in round d > 1 required some shrub between it and its smaller anchor to be removed in round d - 1.
For shrub i, let j be the nearest shrub to its left with a strictly smaller reading.
- If no such
jexists, every shrub to the left is>=the current one, so shrubiis never removed: its round is0. - Otherwise every shrub strictly between
jandihas reading>= readings[i]. As long as any of them survives, the left neighbor ofiis one of them and is not smaller, soistays. Once they are all gone, the left neighbor is at or beforej, whose reading is always<= readings[j] < readings[i], soiis removed in the very next round.
So round[i] = 1 + max(round of shrubs between j and i), or 1 if none lie between them.
A monotonic stack handles this in one pass. It holds (reading, round) pairs with strictly increasing readings. For each new shrub:
- Pop all entries with reading
>=the current reading, tracking the maximum round among them. - If the stack is non-empty, its top is
j, and the current round ismax_popped + 1; otherwise it is0. - Push
(reading, round).
Why it is correct
Shrubs between j and i that were popped earlier were popped by some shrub k also between j and i, and round[k] is strictly greater than the rounds of everything k popped. So the maximum over the popped entries in the current step equals the maximum over all shrubs between j and i. Using <= in the pop condition handles equal readings: an equal shrub does not remove its neighbor and cannot act as the strictly smaller anchor.
Complexity
- Time: O(n), since each shrub is pushed once and popped at most once.
- Space: O(n) for the stack in the worst case (strictly increasing readings).
Reference solution
from typing import List
def pruning_rounds(readings: List[int]) -> int:
# Stack of (reading, round in which this shrub is removed); readings strictly increasing.
stack = []
best = 0
for value in readings:
day = 0
# Pop shrubs that are >= current; they cannot be the smaller anchor on the left.
# The current shrub outlasts all of them.
while stack and value <= stack[-1][0]:
day = max(day, stack.pop()[1])
if stack:
# A strictly smaller shrub exists to the left, so this one is removed
# the round after everything between them is gone.
day += 1
else:
# No strictly smaller shrub on the left: never removed.
day = 0
best = max(best, day)
stack.append((value, day))
return best