您的位置:首页 > 其它

[Leetcode] Largest Rectangle in Histogram

2012-12-04 23:49 411 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: