44ms Easy Solution | C++
This method is not very intuitive ..i did this with some previous knowledge of similar problems\n\nclass Solution {\npublic:\n    int largestRectangleArea(vecto
This method is not very intuitive ..i did this with some previous knowledge of similar problems\n```\nclass Solution {\npublic:\n    int largestRectangleArea(vector<int>& heights) {\n        // this problem should be solved using stack .\n        /* whenever you see a monotonic increase in the input which      \n\t\twould yield  the answer closest to \n\t\trequired you gotta understand that you should\n\t\tuse  a stack datastructure to  calculate the answer*/\n        // so let\'s begin by creating a stack and ans that we return \n        stack<int> st;\n        int ans=0;\n        //just to make our code understand better i\'m adding 0 at the end of\n        //heights\n        heights.push_back(0);\n        /* how do you add the elements to the stack ? mmmmmm? okay \n\t\tlet us think \n        way ,lets add all the ascending order elements to the stack so that \n\t\tit\n        looks like climbing steps .coz in that way we can have atleast the \n\t\tmin\n        size step * number of elements in the stack .if we encounter any \n\t\tbar with         lesser height than the top element of the stack ,we \n\t\twill compute the \n        existing stack element area and pop the top element just to check \n\t\tif the \n        current top < bar height ,if it is so ..add it to the stack ..i think it        \n\t\twill be more clearer if you go through the code*/\n         //very important note .. we are adding indices ..not the values\n        for(int i=0;i<heights.size();i++){\n            while(!st.empty() && heights[st.top()]>heights[i]){\n                //here we are checking if stack is empty or if we encounter \n\t\t\t\tany \n                // number that doesn\'t satisfy our stack filling property\n                int top=heights[st.top()];\n                st.pop();\n                int ran=st.empty()?-1:st.top();\n                //this is to check if stack is empty, if so we will just take the                 //index\n                ans=max(ans,top*(i-ran-1));\n                // this is just to take the max area covered so far\n            }\n            st.push(i);\n            //we push into the stack as long as it satsifies our stack condition\n        }\n        return ans;\n        // i know it\'s not a very clever explanation ..but i tried to explain \n        //whatever i can ...HAPPY CODING!!\n    }\n};\n```\nHere in this problem stack can be replaced with vector and we can use popback and back methods which can implement the same functionality as stack but that one would take more time than the stack method\n
['Stack', 'C', 'C++']