Python | Easy
see the Successfully Accepted Submission\n\n# Code\n\nfrom collections import deque\n\nclass Solution:\n    def histogram(self, heights):\n        n = len(heigh
[see the Successfully Accepted Submission](https://leetcode.com/problems/maximal-rectangle/submissions/1230875459/?source=submission-ac)\n\n# Code\n```\nfrom collections import deque\n\nclass Solution:\n    def histogram(self, heights):\n        n = len(heights)\n        st1 = deque()\n        st2 = deque()\n        left = [0] * n\n        right = [0] * n\n\n        # Previous smaller element\n        for i in range(n):\n            num = heights[i]\n\n            while st1 and heights[st1[-1]] > num:\n                st1.pop()\n\n            if not st1:\n                left[i] = -1\n            else:\n                left[i] = st1[-1]\n\n            st1.append(i)\n\n        # Next greater element\n        for i in range(n - 1, -1, -1):\n            num = heights[i]\n\n            while st2 and heights[st2[-1]] >= num:\n                st2.pop()\n\n            if not st2:\n                right[i] = n\n            else:\n                right[i] = st2[-1]\n\n            st2.append(i)\n\n        max_area = 0\n        for i in range(n):\n            area = (right[i] - left[i] - 1) * heights[i]\n            max_area = max(max_area, area)\n\n        return max_area\n\n    def maximalRectangle(self, matrix):\n        if not matrix:\n            return 0\n\n        n = len(matrix)\n        m = len(matrix[0])\n        curr_row = [0] * len(matrix[0])\n        max_area = 0\n\n        for i in range(n):\n            for j in range(m):\n                if matrix[i][j] == \'1\':\n                    curr_row[j] += 1\n                else:\n                    curr_row[j] = 0\n\n            curr_max = self.histogram(curr_row)\n            max_area = max(max_area, curr_max)\n\n        return max_area\n\n```\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python']