84. Largest Rectangle in Histogram
problem description
Input: [2,1,5,6,2,3]
Output: 10algorithm thought
code
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> st;
heights.push_back(0);
int res=0;
for(int i=0;i<heights.size();){
if((st.empty())||heights[i]>=heights[st.top()]){
st.push(i);
++i;
}else{
int t=st.top();st.pop();
if(st.empty()){
//cout<<heights[t]<<'-'<<i<<' ';
res=max(res,heights[t]*i);
}else{
//cout<<heights[t]<<'-'<<i-st.top()-1<<' ';
res=max(res,heights[t]*(i-st.top()-1));
}
}
}
return res;
}
};algorithm analysis
Last updated

