LeetCode解题 84:Largest Rectangle in Histogram
Problem 84: Largest Rectangle in Histogram [Hard]
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
Example:
Input: [2,1,5,6,2,3]
Output: 10
来源:LeetCode
解题思路
1. 分治法
在一组不同高度的柱子中求取最大面积,可以通过将其中的最矮柱子作为分割点进行分治,公式为:
m a x a r e a ( 0 , N − 1 ) = max { m a x a r e a ( 0 , i n d e x ∗ − 1 ) , N ∗ h e i g h t s [ i n d e x ∗ ] , m a x a r e a ( i n d e x ∗ + 1 , N − 1 ) } max_{area}(0, N-1) = \max\{max_{area}(0, index^*-1), N * heights[index^*], max_{area}(index^*+1, N-1) \} maxarea(0,N−1)=max{
maxarea(0,index∗−