~0ms🔰we have to beat 🥲100% of  java Users 🦾 🔰Height-Based Dynamic Programming Approach🔰
The method used to solve this problem involves iterating through each row of the matrix and updating arrays to keep track of heights, left boundaries, and right
1. > The method used to solve this problem involves iterating through each row of the matrix and updating arrays to keep track of heights, left boundaries, and right boundaries of each column. Then, for each row, calculate the maximum rectangle area using the updated arrays. Finally, return the maximum rectangle area found. This method efficiently finds the maximal rectangle in the given matrix\n\nThe code starts by checking if the input matrix is valid (not null and non-empty), returning 0 if it\'s not.\nIt initializes variables for the number of rows (m) and columns (n) in the matrix.\nThree arrays are created to keep track of heights, left boundaries, and right boundaries of each column.\nThe right boundaries array is initialized to be the length of the matrix\'s columns.\nThe main loop iterates through each row of the matrix.\nFor each row, it updates the heights and left boundaries arrays using the updateHeightsAndLeftBoundaries method.\nIt then updates the right boundaries array using the updateRightBoundaries method.\nAfter updating the boundaries, it calculates the maximum rectangle area for the current row using the calculateMaxRectangle method.\nFinally, it returns the maximum rectangle area found.\nAdditional methods are defined to update heights and boundaries and calculate the maximum rectangle area based on the updated boundaries.\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n![Screenshot 2023-06-07 065915.png](https://assets.leetcode.com/users/images/bd57a1e1-18d6-44c7-b8bd-7835e89a1b3d_1712967884.8473282.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int maximalRectangle(char[][] matrix) {\n        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n            return 0;\n        }\n\n        int m = matrix.length;\n        int n = matrix[0].length;\n\n        int[] heights = new int[n];\n        int[] leftBoundaries = new int[n];\n        int[] rightBoundaries = new int[n];\n        Arrays.fill(rightBoundaries, n);\n\n        int maxRectangle = 0;\n\n        for (int i = 0; i < m; i++) {\n            int left = 0;\n            int right = n;\n\n            updateHeightsAndLeftBoundaries(matrix[i], heights, leftBoundaries, left);\n\n            updateRightBoundaries(matrix[i], rightBoundaries, right);\n\n            maxRectangle = calculateMaxRectangle(heights, leftBoundaries, rightBoundaries, maxRectangle);\n        }\n\n        return maxRectangle;\n    }\n\n    private void updateHeightsAndLeftBoundaries(char[] row, int[] heights, int[] leftBoundaries, int left) {\n        for (int j = 0; j < heights.length; j++) {\n            if (row[j] == \'1\') {\n                heights[j]++;\n                leftBoundaries[j] = Math.max(leftBoundaries[j], left);\n            } else {\n                heights[j] = 0;\n                leftBoundaries[j] = 0;\n                left = j + 1;\n            }\n        }\n    }\n\n    private void updateRightBoundaries(char[] row, int[] rightBoundaries, int right) {\n        for (int j = rightBoundaries.length - 1; j >= 0; j--) {\n            if (row[j] == \'1\') {\n                rightBoundaries[j] = Math.min(rightBoundaries[j], right);\n            } else {\n                rightBoundaries[j] = right;\n                right = j;\n            }\n        }\n    }\n\n    private int calculateMaxRectangle(int[] heights, int[] leftBoundaries, int[] rightBoundaries, int maxRectangle) {\n        for (int j = 0; j < heights.length; j++) {\n            int width = rightBoundaries[j] - leftBoundaries[j];\n            int area = heights[j] * width;\n            maxRectangle = Math.max(maxRectangle, area);\n        }\n        return maxRectangle;\n    }\n}\n```
['Java']