【ATT】Largest Rectangle in Histogram
计算
int largestRectangleArea(vector<int> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function stack<int> st; height.push_back(0); //加入0之后,起哨兵的作用,这样即使矩形是递增的,也可以将面积计算出来(会走到else语句) int max_area = 0; int i=0; while(i<height.size()) { if(st.empty()||height[st.top()]<=height[i]) st.push(i++); else{ int tp = st.top(); st.pop(); int cur_area = height[tp]*(st.empty()?i:i-st.top()-1); //当!st.empty(), [st.top()+1,i-1]范围内的矩形高度大于height[tp],empty时:[0,i-1]范围内矩形高度大于height[tp] max_area = max(max_area,cur_area); } } return max_area; }