Largest Rectangle in Histogram
Updated
Problem statement
You are given an integer array heights. It describes a histogram: a row of bars where every bar has width 1. Return the area of the largest rectangle you can form from one or more bars that sit next to each other.
Function
largestRectangleArea(heights: int[]) → intExamples
Example 1
heights = [2,1,5,6,2,3]return = 10The bars of heights 5 and 6 sit next to each other. Together they form a rectangle of height 5 and width 2. No rectangle is larger, so the answer is its area, ten.
Example 2
heights = [2,4]return = 4The best area is 4, achieved either by the second bar alone or by both bars at height 2.
Example 3
heights = [0]return = 0The only bar has height 0. No rectangle with a positive area exists, so the answer is zero.
Constraints
1 <= heights.length <= 100000.0 <= heights[i] <= 10000.
More Airwallex interview questions
Similar questions at other companies
- Trapping Rain WaterBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Count Visible People to the LeftGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Trapping Rain WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Confirmed Profiling Events with First-Observed StartsAnthropic · PHONE SCREEN · ONSITE INTERVIEWAnthropic interview questions
- Rain Kept Between WallsDocuSign · PHONE SCREENDocuSign interview questions
- Rain Water Held Between BarsAdobe · PHONE SCREENAdobe interview questions
- Largest Rectangle in a HistogramDecagon · PHONE SCREENDecagon interview questions
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Next Taller Building to the RightTemu · PHONE SCREENTemu interview questions