less than 1 minute read

<-H 84> Largest Rectangle in Histogram

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;
        heights.push_back(0);
        int result = 0;
        for(int i = 0; i != heights.size(); ) {
            if(s.empty() || heights[i] > heights[s.top()])
                s.push(i++);
            else {
                int temp = s.top();
                s.pop();
                result = max(result, heights[temp] * (s.empty() ? i : i - s.top() - 1));
            }
        }
        return result;
    }
};