class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int q[100000] = {-1};
int w[100000];
int res = 0;
int top = 0;
for (int i = 0; i <= height.size(); ++i)
{
int h;
i == height.size() ? h = 0: h = height[i];
if (h > q[top])
{
q[++top] = h;
w[top] = 1;
}
else
{
int cnt = 0;
while (h <= q[top])
{
res = max(res, (w[top] + cnt) * q[top]);
cnt += w[top--];
}
q[++top] = h;
w[top] = cnt + 1;
}
}
return res;
}
};[Leetcode] Largest Rectangle in Histogram
最新推荐文章于 2019-04-10 21:02:20 发布
本文介绍了一个使用C++实现的解决方案,解决如何找到给定高度数组中的最大矩形面积问题。通过栈技术和动态规划,实现了一个高效的算法来解决此经典问题。
7738

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



