
Problem
Solution
Submissions
Maximal Rectangle
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C program to find the largest rectangle containing only 1's in a binary matrix. Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example 1
- Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
- Output: 6
- Explanation:
- The matrix has several rectangles of 1's.
- The maximal rectangle is at positions forming a 2x3 rectangle.
- This rectangle has area = 2 * 3 = 6.
- Therefore, maximal rectangle area = 6.
- The matrix has several rectangles of 1's.
Example 2
- Input: matrix = [["0"]]
- Output: 0
- Explanation:
- The matrix contains only one element which is "0".
- No rectangle of 1's can be formed.
- Therefore, maximal rectangle area = 0.
- The matrix contains only one element which is "0".
Constraints
- rows == matrix.length
- cols == matrix[i].length
- 1 ≤ row, cols ≤ 200
- matrix[i][j] is '0' or '1'
- Time Complexity: O(rows * cols)
- Space Complexity: O(cols)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use the concept of largest rectangle in histogram for each row
- For each row, calculate heights of consecutive 1's ending at that row
- Apply largest rectangle in histogram algorithm on these heights
- Use stack-based approach to find maximum rectangle efficiently
- Update heights array: if current cell is '0', height becomes 0, else increment