Find the Nearest Taller Building to the Left
Stacks & QueuesAuto-graded · 17 test casesLast updated
Tests the ability to use a monotonic stack to efficiently find, for each building in a skyline, the index of the nearest building to its left that is strictly taller.
Nearest Taller Building to the Left
You are given a list of building heights along a street. For each building, you want to find the nearest building to its left that is strictly taller than it.
Given a list heights of n non-negative integers, return a list result of length n where:
result[i]is the index of the nearest building to the left of buildingithat has a strictly greater height thanheights[i].- If no such building exists (either
i == 0or all buildings to the left are not strictly taller), setresult[i] = -1.
Constraints
1 <= n <= 20000 <= heights[i] <= 10^6- The returned list must have exactly
nelements. - Indices are 0-based.
- Equal heights do not qualify — the neighbour must be strictly taller.
Examples
Example 1
Input: nearest_taller_left([3, 1, 4, 1, 5, 9, 2, 6])
Output: [-1, 0, -1, 2, -1, -1, 5, 5]
Example 2
Input: nearest_taller_left([5, 5, 5, 5])
Output: [-1, -1, -1, -1]
Example 3
Input: nearest_taller_left([7])
Output: [-1]
Hints
Hint 1
Think about what information you need to keep around as you scan left to right — do you need every previous building, or can you discard some?
Hint 2
If building A is to the left of building B, and A is not taller than B, can A ever be the answer for any building to the right of B?
Hint 3
Maintain a stack of indices whose heights are in strictly decreasing order from bottom to top; pop elements that are not taller than the current building before checking the top.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
As we scan buildings left to right, we need the nearest previous building that is strictly taller. A brute-force approach checks every building to the left for each position — O(n²) in the worst case.
The key observation for a monotonic stack: if building A is to the left of building B, and heights[A] <= heights[B], then A can never be the answer for any building to the right of B. Any future building would encounter B first, and B is at least as tall as A. So A is permanently useless once we pass B.
This means we only need to keep a stack of candidate buildings whose heights are strictly decreasing from bottom (leftmost) to top (rightmost). Whenever a new building i arrives:
- Pop all stack entries whose height is
<= heights[i](they can never serve as a "taller" answer forior anything further right). - The top of the stack (if any) is the nearest strictly taller building — record its index.
- Push
ionto the stack.
Correctness
At the moment we process index i, the stack contains exactly those indices to the left of i whose heights form a strictly decreasing sequence. Every index that was popped was not strictly taller than heights[i], and it also cannot help future indices (blocked by i). The remaining top is therefore the closest strictly taller predecessor.
Complexity
- Time: O(n) — each index is pushed and popped at most once.
- Space: O(n) — the stack holds at most n indices.
from typing import List
def nearest_taller_left(heights: List[int]) -> List[int]:
n = len(heights)
result = [-1] * n
stack = [] # indices, strictly decreasing heights from bottom to top
for i in range(n):
# Remove indices that are not strictly taller than the current building
while stack and heights[stack[-1]] <= heights[i]:
stack.pop()
# Top of stack is the nearest strictly taller building to the left
result[i] = stack[-1] if stack else -1
stack.append(i)
return result