方法一:栈
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int res=0;
stack<int> st;
st.push(-1);
for(int i=0;i<heights.size();i++){
while(st.top()!=-1 && heights[st.top()]>=heights[i]){
int tmp=st.top();
st.pop();
res=max(res, heights[tmp]*(i-st.top()-1));
}
st.push(i);
}
while(st.top()!=-1){ //遍历结束时,处理栈中剩下的
int tmp=st.top();
st.pop();
int t=heights.size()-st.top()-1;
res=max(res, heights[tmp]*t);
}
return res;
}
};