KatInterview

Maximum Visible Buildings on a Skyline Strip

Stacks & QueuesAuto-graded · 17 test cases
HardAuto-gradedStacks & Queuesmonotonic stackarrayssimulation

Last updated

Tests ability to use a monotonic stack to compute, for each building, how many previous buildings it "blocks" and find the building that maximally reduces the visible skyline count.

Maximum Visible Buildings on a Skyline Strip

You are given a row of buildings photographed from the side. Each building is represented by its integer height. Buildings are indexed left to right from 0 to n-1.

A building at index j is visible from the left if no building to its left has a height greater than or equal to j's height. In other words, building j is visible if and only if it is strictly taller than all buildings that appear before it (indices 0 through j-1). Building 0 is always visible.

A demolition candidate is a single building whose removal causes the greatest increase in the number of visible buildings. When a building is removed, the remaining buildings close the gap (i.e., their relative order is preserved) and visibility is recomputed from scratch on the resulting sequence.

Your task is to return a list [idx, gain] where:

  • idx is the 0-based index (in the original array) of the demolition candidate.
  • gain is the increase in the number of visible buildings after removing that building.

Tie-breaking rules:

  • If multiple buildings produce the same maximum gain, choose the one with the smallest index.
  • If no removal increases the number of visible buildings (every building's removal results in the same or fewer visible buildings), return [-1, 0].

How visibility works

Given heights [3, 1, 4, 2, 5]:

  • Building 0 (h=3): always visible. Visible count so far: 1
  • Building 1 (h=1): 3 ≥ 1, not visible.
  • Building 2 (h=4): 4 > 3, visible.
  • Building 3 (h=2): 4 ≥ 2, not visible.
  • Building 4 (h=5): 5 > 4, visible.

Total visible: 3 (indices 0, 2, 4).

What "gain" means

If removing building i changes the visible count from V to V', then gain = V' - V. We want the building i that maximises this gain (with smallest-index tie-break).

Constraints

  • 1 ≤ len(heights) ≤ 2000
  • 1 ≤ heights[i] ≤ 10^6
  • All heights are positive integers.
  • The answer index is always a valid index in the original array (or -1 if no gain is possible).

Examples

Example 1

Input: best_demolition_candidate([3, 1, 4, 2, 5])

Output: [-1, 0]

Example 2

Input: best_demolition_candidate([5, 5, 5, 5])

Output: [-1, 0]

Example 3

Input: best_demolition_candidate([1, 3, 2, 5, 4, 6])

Output: [-1, 0]

Hints

Hint 1

First compute the baseline visible count using a running maximum: building j is visible iff heights[j] > max(heights[0..j-1]).

Hint 2

For each building i, think about what changes when it is removed: only buildings that were previously blocked solely because of building i can become newly visible. A monotonic stack of indices (strictly decreasing heights) tracks exactly which buildings form the 'visibility frontier'.

Hint 3

For a building i on the visibility stack, the buildings it 'blocks' are those between i and the next taller building on the stack whose heights are strictly greater than the predecessor of i on the stack but ≤ heights[i]. Count these efficiently as you build the monotonic stack, then the gain for removing i equals that count minus the visibility status of i itself.