84. Largest Rectangle in Histogram

这篇博客讨论了如何解决计算机科学中的最大矩形面积问题,特别是在直方图中找到最大的矩形区域。博主提供了两种算法:暴力求解和使用栈的方法。暴力求解由于时间复杂度过高而未通过,而栈的解决方案通过维护高度的递增顺序有效地解决了问题。博客还包含了详细的代码实现和动画演示,解释了如何通过入栈和出栈操作找到最大矩形的边界并计算其面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

84.Largest Rectangle in Histogram

Given an array of integers heights representing the histogram’s bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
image

Example 1:

Input: height = [2,1,5,6,2,3]
Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.

Related Topics

  • Array
  • Stack

Similar Questions

No.TitleDifficulty
11Container With Most Water☆☆☆
42Trapping Rain Water☆☆☆
85Maximal Rectangle☆☆☆☆

Solution

Approach 1: Brute Force

以该点的高度为中心,左右探测。

时间超时。

class Solution {
public:
    int largestRectangleArea(vector<int>& height) {
        int res = 0;
        int n = height.size();
        if (n <= 0) return 0;
        for (int i = 0; i < n; i++) {
            int H = height[i];
            int L = i;
            int R = i;
            while (L >= 0 && height[L] >= H) L--;
            while (R <  n && height[R] >= H) R++;
            res = max(res, H*(R-L-1));
        }
        return res;
    }
};
Approach 2: Using Stack

用栈跟踪左边界。

中间夹着的永远是凸型。

入栈出栈动画及面积计算过程见文末。

class Solution {
   public:
    int largestRectangleArea(vector<int>& hist) {
        int n = hist.size();
        // Create an empty stack. The stack holds indexes
        // of hist[] array. The bars stored in stack are
        // always in increasing order of their heights.
        stack<int> s;

        int max_area = 0;   // Initialize max area
        int tp;             // To store top of stack
        int area_with_top;  // To store area with top bar
                            // as the smallest bar

        // Run through all bars of given histogram
        int i = 0;
        while (i < n) {
            // If this bar is higher than the bar on top
            // stack, push it to stack
            if (s.empty() || hist[i] > hist[s.top()]) {
                s.push(i++);
            // If this bar is lower than top of stack,
            // then calculate area of rectangle with stack
            // top as the smallest (or minimum height) bar.
            // 'i' is 'right index' for the top and element
            // before top in stack is 'left index'
            } else {
                tp = s.top();  // store the top index
                s.pop();       // pop the top

                // Calculate the area with hist[tp] stack
                // as smallest bar
                area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);

                // update max area, if needed
                max_area = max(max_area, area_with_top);
            }
        }

        // Now pop the remaining bars from stack and calculate
        // area with every popped bar as the smallest bar
        while (s.empty() == false) {
            tp = s.top();
            s.pop();
            area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);
            max_area = max(max_area, area_with_top);
        }

        return max_area;
    }
};

可以在数组最后加入0高度,这样处理即可保证,遍历到最后一个元素时,栈中所有元素均可弹出。

class Solution {
public:
    int largestRectangleArea(vector<int>& hist) {
        hist.push_back(0);
        int n = hist.size();
        stack<int> s;
        int max_area = 0;   // Initialize max area
        int tp;             // To store top of stack
        int area_with_top;  // To store area with top bar
                            // as the smallest bar
        int i = 0;
        while (i < n) {
            if (s.empty() || hist[i] > hist[s.top()])
                s.push(i++);
            else {
                tp = s.top();
                s.pop();
                area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);
                max_area = max(max_area, area_with_top);
            }
        }
        return max_area;
    }
};

Approach 2 动画演示:

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值