/84. Largest Rectangle in Histogram

84. Largest Rectangle in Histogram

Hard
Arrays49.5% acceptance

Given an integer array bar_heights representing the heights of bars in a histogram, where each bar has a width of 1 unit, compute and return the maximum area of any rectangle that can be formed within the histogram. The rectangle must be fully contained within consecutive bars.

Example 1

Input: [3, 1, 3, 2, 2]

Output: 6

Explanation: The largest rectangle has area 6 (bars 2-4, height 2).

Example 2

Input: [1, 2, 3, 4, 5]

Output: 9

Explanation: The largest rectangle has area 9 (bars 3-5, height 3).

Example 3

Input: [5, 5, 5, 5]

Output: 20

Explanation: The largest rectangle has area 20 (all bars, height 5).

Constraints

  • 1 <= len(bar_heights) <= 100000
  • 0 <= bar_heights[i] <= 10000 for all valid i
Python (current runtime)

Case 1

Input: [4, 2, 0, 3, 2, 5]

Expected: 6

Case 2

Input: [6, 2, 5, 4, 5, 1, 6]

Expected: 12

Case 3

Input: [2, 2, 2, 2, 2]

Expected: 10

Case 4

Input: [1, 0, 1, 0, 1]

Expected: 1

Case 5

Input: [0, 0, 0, 0]

Expected: 0