People stand in a line, all facing right. Person i looks to the right and can see person j if nobody standing between them is tall enough to block the view. Every person between positions i and j must be shorter than both person i and person j.
Two observations drive every approach below. First, if person i encounters someone taller than themselves while looking right, that tall person is the last one they can see. Nobody behind a taller person is visible, because the taller person blocks the view. Second, the people that person i can see form a sequence of strictly decreasing heights, followed by at most one person who is taller than person i.
The core question becomes: for each person, how many people form a visible chain to their right before a taller person blocks everything?
1 <= n <= 10^5 → With up to 100,000 people, we need O(n log n) or better. An O(n^2) brute force that checks all pairs would hit 10 billion operations in the worst case.1 <= heights[i] <= 10^5 → All heights are positive integers. No zeros or negatives to worry about.Do exactly what the problem says. For each person i, scan every person j to the right and check whether all people between i and j are shorter than both heights[i] and heights[j]. If so, person i can see person j.
The two observations above let us scan more directly. As person i scans to the right, they keep seeing people as long as those people are shorter than heights[i]. The first person who is as tall or taller is the last visible one, since that person blocks the view of everyone behind them. So for each person, we scan right, count everyone shorter than heights[i] that is also taller than everyone in between, add one more if we reach a taller person, then stop.
answer of length n, initialized to zeros.i from 0 to n-1:i and the current person j.j from i+1 to n-1:i and j (exclusive of endpoints) is less than min(heights[i], heights[j]), person i can see person j. Increment answer[i].heights[j] >= heights[i], stop scanning. This person blocks the view of everyone behind.answer.The brute force repeats work: each person scans rightward through the array and revisits the same shorter people that earlier persons already scanned past. The next approach processes people so that each one is pushed and popped at most once, bringing the total work down to linear.
When person i looks right, they see a sequence of people with strictly decreasing heights, because any taller person would have blocked the shorter ones behind it. At the end of this decreasing sequence, they might also see one person who is taller than themselves, which blocks everything beyond.
A monotonic decreasing stack captures this "decreasing sequence followed by a taller blocker" pattern directly. Processing people from right to left, we maintain a stack of heights in decreasing order. For each person i:
heights[i]. Each popped person is someone that person i can see (they form that decreasing sequence).i, so person i can also see that person.heights[i] onto the stack.The stack maintains the invariant that the heights it holds are in decreasing order from bottom to top, which matches the visibility rule. When person i looks right, the people they can see are exactly those in a decreasing run (the popped elements) plus the first person who breaks the run by being taller (the stack top after popping).
Popping is also why each shorter person is counted only once. Once person i pops a shorter person to their right, that person can never be visible to anyone further left, because person i is taller and now blocks them. So every height enters the stack once and leaves at most once, which bounds the total work at 2n and gives linear time.
answer of length n, initialized to zeros.n-1 down to 0):heights[i], pop from the stack and increment answer[i]. Each pop represents a shorter person that person i can see.answer[i] by 1. The stack top is the first taller person, and person i can see them too.heights[i] onto the stack.answer.