1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &h) { 4 5 stack<int> stk; 6 int index = 0, maxArea = 0; 7 h.push_back(0); 8 9 while(index < h.size()) { 10 11 if(stk.empty() || h[stk.top()] <= h[index]){ 12 13 stk.push(index); 14 index++; 15 } 16 17 else { 18 int prev = stk.top(); 19 stk.pop(); 20 maxArea = max(maxArea, h[prev] * ( stk.empty() ? index :index-stk.top() - 1 )); 21 } 22 } 23 return maxArea; 24 } 25 };