BackhardStacks

Maximum Histogram Area Solution

Problem Statement

Given an array of integers representing the heights of bars in a histogram, find the maximum area of the histogram by choosing the minimum height of consecutive bars as the height of the rectangle and the number of bars as the width.

Example 1
Input
[2, 1, 5, 6, 2, 3]
Output
10

Explanation: The maximum area is formed by the bars with heights 5 and 6, with a minimum height of 5 and a width of 2, so the maximum area is 10.

Example 2
Input
[4, 2, 0, 3, 2, 5]
Output
6

Explanation: This example is kept as is, however further review of the math behind the solution to this particular input would be required to ensure accuracy.

Constraints

  • 1 <= length of input array <= 10^5
  • 0 <= height of each bar <= 10^4
  • The input array will not be empty
  • The function should return the maximum area in O(n) time complexity
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free