Created
February 19, 2018 19:55
-
-
Save zhuangh/3d1223f718659064ab316fa5a377dbc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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