Largest Rectangle in Histogram
class Solution {
public:
int largestRectangleArea(vector<int>& height) {
stack<pair<int,int> > st;
int maxarea = 0;
for (int i = 0; i <= height.size(); ++i) {
int h = i == height.size() ? -1:height[i];
if (st.empty() || st.top().first < h) {
st.push(make_pair(h, i));
} else {
int lastp = i;
while (!st.empty() && st.top().first >= h) {
maxarea = max(maxarea, (i - st.top().second) * st.top().first);
lastp = st.top().second;
st.pop();
}
st.push(make_pair(h, lastp));
}
}
return maxarea;
}
};