[C++] Largest Rectangle in Histogram with Stack
Description\n\nGet the Largest Area of Rectangle in a Histogram with a Monotonic Stack.\n\nProgram\n\ncpp []\nclass Solution {\npublic:\n    int largestRectangl
**Description**\n\nGet the Largest Area of Rectangle in a Histogram with a Monotonic Stack.\n\n**Program**\n\n```cpp []\nclass Solution {\npublic:\n    int largestRectangleArea(vector<int>& heights) {\n        int result = 0;\n        stack<pair<int, int>> stack;\n        for (int i = 0; i < heights.size(); i++) {\n            int start = i;\n            while (!stack.empty() && stack.top().second > heights[i]) {\n                int index = stack.top().first;\n                int width = i - index;\n                int height = stack.top().second;\n                stack.pop();\n                result = max(result, height * width);\n                start = index;\n            }\n            stack.push({start, heights[i]});\n        }\n        while (!stack.empty()) {\n            int width = heights.size() - stack.top().first;\n            int height = stack.top().second;\n            stack.pop();\n            result = max(result, height * width);\n        }\n        return result;\n    }\n};\n```
['Stack', 'Monotonic Stack', 'C++']