84. Largest Rectangle in Histogram
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.
求最大矩形的面积
方法一:暴力来leetcode上也能通过,方法也非常的好理解,就是遍历到每一个高度,向左和向右遍历,找到低于该高度的边界顺便计算矩形的宽度,进而计算面积,最终找到面积的最大值。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if(heights.size()==0){
return 0;

解决84题——Largest Rectangle in Histogram,通过遍历和使用栈两种方法找到非负整数数组表示的直方图中最大矩形面积。方法一是暴力遍历,方法二是借助栈保持高度非递减,找到最大矩形的左右边界。提供代码实现并欢迎指正。
最低0.47元/天 解锁文章
4463

被折叠的 条评论
为什么被折叠?



