Easy Solution using Stacks✅💯
\n\n# Complexity\n- Time complexity:\nO(nm) + O(n2m)\n\n- Space complexity:\nO(n*m) + O(n)\n\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int largestRectan
![image.png](https://assets.leetcode.com/users/images/3704751d-aafb-41ed-9934-27476d34909f_1724608193.2074795.png)\n\n# Complexity\n- Time complexity:\n$$O(n*m) + O(n*2m)$$\n\n- Space complexity:\n$$O(n*m) + O(n)$$\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int largestRectangleArea(vector<int>& heights) {\n        stack<int> st;\n        int maxArea=0;\n        int n=heights.size();\n        int nse;\n        int pse;\n        int ele;\n        for(int i=0;i<n;i++){\n            while(!st.empty() && heights[st.top()]>=heights[i]){\n                ele=st.top();\n                st.pop();\n                nse=i;\n                pse=st.empty()?-1:st.top();\n                maxArea=max(maxArea, heights[ele]*(nse-pse-1));\n            }\n            st.push(i);\n        }\n        while(!st.empty()){\n            nse=n;\n            ele=st.top();\n            st.pop();\n            pse=st.empty()?-1:st.top();\n            maxArea=max(maxArea, heights[ele]*(nse-pse-1));\n        }\n        return maxArea;\n    }\n    int maximalRectangle(vector<vector<char>>& matrix) {\n        int r = matrix.size();\n        if(r == 0) return 0;\n        int c = matrix[0].size();\n\n        vector<int> ps(c,0);\n        int maxi = 0;\n\n        for(int i = 0; i < r; ++i){\n            for(int j = 0; j < c; ++j){\n                if(matrix[i][j] == \'1\') ps[j]++;\n                if(matrix[i][j] == \'0\') ps[j] = 0;\n            }\n            maxi = max(maxi, largestRectangleArea(ps));\n        }\n        return maxi;\n    }\n};\n```
['C++']