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;
}
};