Skip to content

Instantly share code, notes, and snippets.

@zhuangh
Created February 19, 2018 19:55
Show Gist options
  • Save zhuangh/3d1223f718659064ab316fa5a377dbc5 to your computer and use it in GitHub Desktop.
Save zhuangh/3d1223f718659064ab316fa5a377dbc5 to your computer and use it in GitHub Desktop.
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int space = 0;
int maxh = 0;
int n = heights.size();
stack<int> s;
int i = 0;
int max_space = 0;
while(i<n){
if( s.empty() || ( heights[s.top()] < heights[i])) {
s.push(i); i++;
}
else{
int idx = s.top();
s.pop();
int space = heights[idx]*( (!s.empty()) ? (i-1-s.top()) : i);
max_space = max(max_space, space);
}
}
while(!s.empty()){
int idx = s.top();
s.pop();
int space = heights[idx]*( (!s.empty()) ? (n-1-s.top()) : n);
max_space = max(space, max_space);
}
return max_space;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment