Largest Square Under Building Heights
Problem statement
You are given nonnegative building heights in heights.
For this exercise, assume the buildings are adjacent unit-width columns on a grid, all starting at the same baseline. The filled cells below each height form a skyline. Find the largest axis-aligned square that fits entirely inside this filled region.
A square of integer side length s fits if there are at least s consecutive columns whose heights are all at least s. Return the maximum possible side length, not its area. Return 0 for an empty skyline or when all columns have height zero.
Function
largestBuildingSquare(heights: int[]) → intExamples
Example 1
heights = [3,3,3,1]return = 3The first three columns contain a 3-by-3 square.
Example 2
heights = [5,1,5]return = 1A tall single column has width 1, and the middle height prevents a larger square.
Example 3
heights = []return = 0No columns means no positive-size square.
Constraints
0 <= heights.length <= 1000.0 <= heights[i] <= 10^9.