FastPrepLargest Square Under Building Heights

Largest Square Under Building Heights

Roblox logoRoblox● MediumNEW GRADONSITE INTERVIEW
Learn

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[]) → int

Examples

Example 1

heights = [3,3,3,1]return = 3

The first three columns contain a 3-by-3 square.

Example 2

heights = [5,1,5]return = 1

A tall single column has width 1, and the middle height prevents a larger square.

Example 3

heights = []return = 0

No columns means no positive-size square.

Constraints

  • 0 <= heights.length <= 1000.
  • 0 <= heights[i] <= 10^9.

More Roblox problems

See Roblox hiring insights
public int largestBuildingSquare(int[] heights) {
    // write your code here
}
heights[3,3,3,1]
expected3
Checking account…