Find Maximum Rectangle in Bar Chart
Problem Statement
You are given an array heights containing n non-negative integers. Each integer heights[i] represents the height of a vertical bar in a bar chart, and every bar is assumed to have a width of 1.
Your task is to find the largest possible rectangular area that can be formed by selecting one or more consecutive bars from the chart. The height of any such rectangle is determined by the minimum height among the chosen bars, and its width is the total number of bars selected.
Return the maximum area found.
Examples
Example 1:
Input: heights = [2, 1, 5, 6, 2, 3]
Output: 10
Explanation: The optimal rectangle is formed by bars with heights 5 and 6. Its height is min(5, 6) = 5, and its width is 2 (two bars). Area = 5 * 2 = 10.
Example 2:
Input: heights = [2, 4]
Output: 4
Constraints
1 <= n <= 1000000 <= heights[i] <= 1000000000- The calculated maximum area will fit within a 64-bit signed integer.
AI interview prep
Core dilemma
The central engineering conflict is efficiently calculating the maximum rectangular area. A naive O(N^2) approach (checking all possible sub-arrays) will exceed time limits for N=100,000, requiring an optimized O(N log N) or O(N) solution.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find Maximum Rectangle in Bar Chart
Problem Statement
You are given an array heights containing n non-negative integers. Each integer heights[i] represents the height of a vertical bar in a bar chart, and every bar is assumed to have a width of 1.
Your task is to find the largest possible rectangular area that can be formed by selecting one or more consecutive bars from the chart. The height of any such rectangle is determined by the minimum height among the chosen bars, and its width is the total number of bars selected.
Return the maximum area found.
Examples
Example 1:
Input: heights = [2, 1, 5, 6, 2, 3]
Output: 10
Explanation: The optimal rectangle is formed by bars with heights 5 and 6. Its height is min(5, 6) = 5, and its width is 2 (two bars). Area = 5 * 2 = 10.
Example 2:
Input: heights = [2, 4]
Output: 4
Constraints
1 <= n <= 1000000 <= heights[i] <= 1000000000- The calculated maximum area will fit within a 64-bit signed integer.