Solution Using Stack(C++)
This problem is particularly  almost similar to Trapping Rainwater Problem.\nHere in this post I would like to highlight both their solutions so one could dop a
This problem is particularly  almost similar to Trapping Rainwater Problem.\nHere in this post I would like to highlight both their solutions so one could dop a comparitive study of the approach and figure out their individual details.\n\n**Trapping Rainwater Problem (Approach Using Stack)**\n\n```\nint trap(vector<int>& height)\n{\n    int ans = 0, current = 0;\n    stack<int> st;\n    while (current < height.size()) {\n        while (!st.empty() && height[current] > height[st.top()]) {\n            int top = st.top();\n            st.pop();\n            if (st.empty())\n                break;\n            int horizontal = current - st.top() - 1;\n            int vertical = min(height[current], height[st.top()]) - height[top];\n            ans += horizontal * vertical;\n        }\n        st.push(current++);\n    }\n    return ans;\n}\n```\n\n*Link: https://leetcode.com/problems/trapping-rain-water/solution/*\n\n**Largest Rectangle Problem (Approach Using Stack)**\n\n\n\n```\nclass Solution {\npublic:\n    int largestRectangleArea(vector<int>& heights) {\n        stack<int>st;\n        heights.push_back(0);\n        int ret=0;\n            for(int i = 0; i < heights.size(); i++)\n            {\n                while(st.size() > 0 && heights[st.top()] >= heights[i])\n                {\n                    int vertical = heights[st.top()];\n                    st.pop();\n                    \n                    int right_upper_bound = st.size() > 0 ? st.top() : -1;\n                    int horizontal=(i-right_upper_bound-1);\n                    if(vertical * horizontal  > ret)\n                        ret = vertical * horizontal;\n                }\n                st.push(i);\n            }\n            \n            return ret;\n        }\n    };\n```\n\nHope this would help to make a comparision and help you to approach similar problems with ease!!
['Stack', 'C']