Find largest rectangle containing only zeros in an N×N binary matrix
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Problem Overview
Finding the largest rectangle containing only zeros in an binary matrix is a classic algorithmic problem. Given a matrix where each element is either a 0 or a 1, the goal is to determine the dimensions and position of the largest rectangle consisting entirely of 0s.
Problem Formulation
Given a binary matrix M of size , each entry is either 0 or 1. The aim is to identify the largest sub-matrix entirely composed of 0s and return its size.
Mathematical Foundation and Algorithmic Approach
Key Observations
- Rectangle as a Sub-matrix: A rectangle is effectively a contiguous sub-matrix determined by four boundaries: top, bottom, left, and right. Identifying the largest such rectangle requires assessing all rows and columns.
- Treat Rows as Histogram Heights: The primary technique involves interpreting consecutive rows of 0s as histogram heights. The challenge then transforms into finding the largest rectangle in a histogram, which can be efficiently solved using a stack-based approach.
Step-by-Step Algorithm
- Initialize Height Array: Start with a height array initialized to zeros of size , which will store the height of histograms (connected zeros vertically) for each column.
- Iterate Through Each Row:
- For each row, update the height array.
- If is 0, increment the height; otherwise, reset it to zero.
- Apply the largest rectangle in histogram technique to the height array to find the maximum area rectangle possible ending at the current row.
- Histogram Rectangle Calculation:
- Use a stack to store indices of the height array.
- Iterate through each column and manage the stack to compute the largest rectangle.
- For each index, while the stack is not empty and the current height is less than the height represented by the index on the top of the stack:
- Pop the value from the stack and calculate the potential maximum area using the current index as the right boundary.
Complexity Analysis
- Time Complexity: The solution involves traversing the matrix, which takes , and applying the histogram rectangle approach, each operation in the stack happens in constant time. Thus, the overall complexity is .
- Space Complexity: The additional space is primarily used for the height array and the stack, leading to complexity.
Technical Illustration
Consider the following matrix:
- For the first row: Update
H = [1, 0, 0, 1]and evaluate the histogram to find the largest rectangle. - For the second row: Update
H = [2, 1, 0, 2]. - Continue similarly for subsequent rows.
- This problem is extendable to rectangular matrices.
- Adapting the algorithm for maximal rectangles with 1s instead of 0s involves changing conditions for updating the height array.
- The algorithm demonstrates the utility of converting traditional 2D problems into manageable 1D problems using histograms, which is a versatile approach in computational geometry and image processing.
Related reading
- Find length of smallest window that contains all the characters of a string in another string
- Find local minima in an array
- Find local minimum in n x n matrix in On time
- Find longest increasing sequence
- Find maximum possible time HHMM by permuting four given digits
- Find minimal Ai2 Bi2 when A and B are sorted
- Find longest repetitive sequence in a string
- find lowest index of a given value in a presorted array

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.