✅ Simple clean stack approach with explanation
Approach\n1. Monotonic Stack: Utilize a monotonic stack to keep track of heights and indices in ascending order. If the height encountered is less than the heig
# Approach\n1. Monotonic Stack: Utilize a monotonic stack to keep track of heights and indices in ascending order. If the height encountered is less than the height at the top of the stack, pop elements from the stack and calculate the area of the rectangles until the current index.\n2. Compute Largest Area: While popping elements from the stack, calculate the area of the rectangle formed by the popped height and the current index, considering the width as the difference between the current index and the index at the top of the stack.\n\n# Complexity\n- Time complexity:\n`O(n)`\n\n- Space complexity:\n`O(n)`\n\n# Code\n```\nclass Solution {\npublic:\n    /* \n    * Approach:\n    * 1. Monotonic Stack: Utilize a monotonic stack to keep track of heights and indices in ascending order. If the height encountered is less than the height at the top of the stack, pop elements from the stack and calculate the area of the rectangles until the current index.\n    * 2. Compute Largest Area: While popping elements from the stack, calculate the area of the rectangle formed by the popped height and the current index, considering the width as the difference between the current index and the index at the top of the stack.\n    * \n    * Complexity:\n    * Time Complexity : O(n)\n    * Space Complexity : O(n)\n    */\n    int largestRectangleArea(vector<int>& heights) \n    {\n        heights.push_back(-1);\n        stack<pair<int, int>> st;\n        int largestArea{};\n        \n        for(int i = 0 ; i < (int)heights.size() ; i++)\n        {\n            int startIdx{i};\n            while(!st.empty() && heights[i] < st.top().first)\n            {\n                auto [height, idx] = st.top();\n                st.pop();\n                largestArea = max(largestArea, height * (i - idx));\n                startIdx = idx;\n            }\n            st.push({heights[i], startIdx});\n        }\n        \n        return largestArea;\n    }\n};\n```\n\n**Please consider upvoting \u2B06\uFE0F if you found this solution helpful! Thank you!**
['Stack', 'Monotonic Stack', 'C++']