直方图最大矩形覆盖-LintCode

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.
样例
给出 height = [2,1,5,6,2,3],返回 10
一刷TLE(捂脸)

#ifndef C122_H
#define C122_H
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    /**
    * @param height: A list of integer
    * @return: The area of largest rectangle in the histogram
    */
    int largestRectangleArea(vector<int> &height) {
        // write your code here
        if (height.size() == 0)
            return 0;
        return largestAreaRecur(height, 0,highVal(height));
    }
    int largestAreaRecur(vector<int> &v, int i,int k)
    {
        int len = v.size();
        int num = 0,count=0;
        if (i > k)
            return 0;
        for (int j = 0; j < len ; ++j)
        {
            if (v[j] >= 0)
            {
                num++;
            }
            else
            {
                count = maxVal(count, num);
                num = 0;
            }
        }
        count = maxVal(num, count);
        for (auto &c : v)
        {
            c--;
        }
        return maxVal(i*count,largestAreaRecur(v,i+1,k));
    }
    int maxVal(int a, int b)
    {
        return a>b ? a : b;
    }
    int highVal(vector<int> v)
    {
        int num = 0;
        for (auto c : v)
        {
            num = c > num ? c : num;
        }
        return num;
    }
};
#endif

another

#ifndef C122_H
#define C122_H
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
class Solution {
public:
    /**
    * @param height: A list of integer
    * @return: The area of largest rectangle in the histogram
    */
    int largestRectangleArea(vector<int> &height) {
        // write your code here
        int res = 0;
        stack<int> s;
        height.push_back(0);
        for (int i = 0; i < height.size(); ++i) {
            if (s.empty() || height[s.top()] < height[i]) s.push(i);
            else {
                int cur = s.top();
                s.pop();
                res = max(res, height[cur] * (s.empty() ? i : (i - s.top() - 1)));
                --i;
            }
        }
        return res;
    }
};
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用单调栈求解直方图最大矩形面积的C++完整代码: ```c++ #include <iostream> #include <stack> using namespace std; int getMaxArea(int hist[], int n) { stack<int> stk; int maxArea = 0, area = 0, i = 0; while (i < n) { if (stk.empty() || hist[stk.top()] <= hist[i]) { stk.push(i++); } else { int top = stk.top(); stk.pop(); area = hist[top] * (stk.empty() ? i : i - stk.top() - 1); if (area > maxArea) { maxArea = area; } } } while (!stk.empty()) { int top = stk.top(); stk.pop(); area = hist[top] * (stk.empty() ? i : i - stk.top() - 1); if (area > maxArea) { maxArea = area; } } return maxArea; } int main() { int hist[] = {6, 2, 5, 4, 5, 1, 6}; int n = sizeof(hist) / sizeof(hist[0]); cout << "The maximum area of the histogram is " << getMaxArea(hist, n) << endl; return 0; } ``` 在上述代码中,我们使用一个单调栈来维护直方图中的柱子。具体来说,我们从左到右遍历直方图中的柱子,如果当前柱子的高度不小于栈顶柱子的高度,则将当前柱子入栈;否则,我们弹出栈顶柱子,计算以该柱子高度为最小高度的矩形面积,更新最大面积值。在弹出栈顶柱子之后,当前柱子继续与栈顶柱子比较,直到当前柱子的高度不小于栈顶柱子的高度或者栈为空为止。 当所有的柱子都遍历完成后,我们需要将栈中剩余的柱子依次弹出,并根据弹出柱子的高度计算以该高度为最小高度的矩形面积,再次更新最大面积值。 最后,我们返回最大面积值即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值