solution.md 4.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
# 柱状图中最大的矩形
<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    vector<int> heights = {2, 1, 5, 6, 2, 3};
    int res;
    res = sol.largestRectangleArea(heights);
    cout << res << endl;
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    int largestRectangleArea(vector<int> &heights)
    {
        if (heights.empty())
            return 0;
        stack<int> st;
        heights.push_back(0);
        int res = 0;
        for (int i = 0; i < heights.size(); i++)
        {
            while (!st.empty() && heights[i] < heights[st.top()])
            {
                int curHeight = heights[st.top()];
                st.pop();
                int width = st.empty() ? i : i - st.top();
                if (width * curHeight > res)
                    res = width * curHeight;
            }
            st.push(i);
        }
        return res;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    int largestRectangleArea(vector<int> &heights)
    {
        int sz = heights.size();
        int ma = 0;
        for (int i = 0; i < sz; i++)
        {
            int len = 1;
            int hei = heights[i];
            int sta = i - 1, en = i + 1;
            while (sta >= 0 && heights[sta] >= hei)
            {
                len++;
                sta--;
            }
            while (en < sz && heights[en] >= hei)
            {
                len++;
                en++;
            }
            ma = max(ma, len * hei);
        }
        return ma;
    }
};
```

### B
```cpp
class Solution
{
public:
    int largestRectangleArea(vector<int> &heights)
    {
        int n = heights.size();

        vector<int> left(n), right(n, n);

        stack<int> mono_stack;

        for (int i = 0; i < n; ++i)
        {

            while (!mono_stack.empty() && heights[mono_stack.top()] >= heights[i])
            {
                right[mono_stack.top()] = i;
                mono_stack.pop();
            }

            left[i] = (mono_stack.empty() ? -1 : mono_stack.top());

            mono_stack.push(i);
        }

        int ans = 0;
        for (int i = 0; i < n; ++i)
        {

            ans = max(ans, (right[i] - left[i] - 1) * heights[i]);
        }
        return ans;
    }
};
```

### C
```cpp
class Solution
{
public:
    int largestRectangleArea(vector<int> &heights)
    {
        int n = heights.size();
        stack<int> index;
        int area = 0;
        for (int i = 0; i < heights.size(); i++)
        {
            if (index.empty() || heights[index.top()] < heights[i])
                index.push(i);
            else
            {
                while (!index.empty() && heights[index.top()] >= heights[i])
                {
                    int tmp = index.top();
                    index.pop();
                    int length = 0;
                    if (index.empty())
                        length = i;
                    else
                        length = i - index.top() - 1;
                    area = max(area, length * heights[tmp]);
                }
                index.push(i);
            }
        }
        while (!index.empty())
        {
            int tmp = index.top();
            index.pop();
            int length = 0;
            if (index.empty())
                length = n;
            else
                length = n - index.top() - 1;
            area = max(area, length * heights[tmp]);
        }
        return area;
    }
};
```