Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- // Time Complexity:- O(N)
- // Space Complexity:- O(N)
- int largestRectangleArea(vector<int>& heights) {
- int n = heights.size();
- stack <int> index; // stack helps in storing increasing elements indices
- int max_area = 0;
- for(int i=0;i<heights.size();i++){
- while(!index.empty() and heights[i]<heights[index.top()]){
- int w = heights[index.top()];
- index.pop();
- int left = index.empty()?-1:index.top();
- int right = i;
- int l = right - left - 1;
- max_area = max(max_area,l*w);
- }
- index.push(i);
- }
- while(!index.empty()){
- int w = heights[index.top()];
- index.pop();
- int left = index.empty()?-1:index.top();
- int right = n;
- int l = right - left - 1;
- max_area = max(max_area,l*w);
- }
- return max_area;
- }
- };
Add Comment
Please, Sign In to add comment