AceYourInterviews
Checking sign-in…

46. Largest Rectangle in Histogram

AdvancedIncreasing stack with equal-height ownership transfer

Choose consecutive histogram bars and a rectangle whose height fits under every chosen bar. Return the greatest possible area over all choices of consecutive bars.

The shortest bar limits a chosen rectangle's height, while the number of chosen bars determines its width. The rectangle cannot skip a short bar in the middle.

Example 1

Input

[2,1,5,6,2,3]

Output

10

Explanation: Bars at indices two and three support height five across width two.

Example 2

Input

[2,2,2]

Output

6

Explanation: Equal-height ownership reaches the full width when the final representative is flushed.

Constraints

  • 0 <= heights.length <= 105
  • 0 <= heights[i] <= 231 - 1
Hints
Hint 1

When does a new bar prove a pending height cannot continue right?

Hint 2

After a pop, which two boundaries enclose its valid span?

Hint 3

Compute height*(i-left-1), transfer equal representatives, and flush with a virtual sentinel.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Pop completed spans at smaller right boundaries

[2,1,5,6,2,3]
10