problem:https://leetcode.com/problems/largest-rectangle-in-histogram
Classic monotonous stack topic. Maintenance monotonically increasing stack, when the stack was found to be smaller than the current number of times, then the top element is the largest (greater than the next element in the stack, is also greater than the current element), it is possible to calculate the current top of the stack high rectangular area, and the comparison is not the greatest.
class Solution { public: int largestRectangleArea(vector<int>& heights) { heights.push_back(0); int n = heights.size(); stack<int> sta; int res = 0; for(int i = 0;i < n;i++) { while(!sta.empty() && heights[i] <= heights[sta.top()]) { int h = heights[sta.top()]; sta.pop(); int left = sta.empty() ? -1 : sta.top(); int s = h * (i - left - 1); res = max(res, s); } // cout << res << " "; sta.push(i); } return res; } };