knakul853

Untitled

Jul 20th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int largestRectangleArea(vector<int>& heights) {
  4.        
  5.         heights.push_back(0);
  6.         int n = (int)heights.size();  
  7.         int ans = 0;
  8.        
  9.         stack<int>st;
  10.        
  11.         for( int i=0; i < n; i++ )
  12.         {
  13.             while( !st.empty() && heights[st.top()] >= heights[i] )
  14.             {
  15.                 int h = heights[st.top()];
  16.                
  17.                 st.pop();
  18.                
  19.                 int w = st.empty() ?  i : i - st.top()-1;
  20.                
  21.                 ans = max( ans, h * w );
  22.             }
  23.            
  24.             st.push(i);
  25.         }
  26.        
  27.         return ans;
  28.     }
  29. };
Add Comment
Please, Sign In to add comment