Given a binary matrix, find the maximum size rectangle binary-sub- matrix with all 1's.

0

 Given a binary matrix, find the maximum size rectangle binary-sub-matrix with all 1's.

Given a binary matrix, find the maximum size rectangle binary-sub- matrix with all 1's.



Cpp Program Code:

Here's the C++ code to find the maximum size rectangle binary sub-matrix with all 1's in a given binary matrix:


The getMaxArea function takes an integer array arr and its size n as input, and returns the maximum rectangular area that can be formed using the bars in the array. It uses a stack to keep track of the indices of bars, and calculates the area of the rectangle formed by each bar and the bars to its left and right. The maximum of these areas is returned as the result.


The maxRectangle function takes a 2D integer array mat representing the binary matrix, and its number of rows R and columns C as input, and returns the maximum size rectangle binary sub-matrix with all 1's. It calculates the height of each bar in the matrix, and applies the getMaxArea function to each row of the matrix to find the maximum area of the rectangle that can be formed using the bars in that row. Finally, it returns the maximum of these areas as the result.


In the main function, we initialize a 2D integer array mat, its number of rows R and columns C, and call the maxRectangle function to find the maximum size rectangle binary sub-matrix with all 1's. The result is printed to the console.


Python Program code:

Here's the Python code to find the maximum size rectangle binary-sub-matrix with all 1's:


This code first calculates the heights of each column by scanning the matrix row by row. It then uses the largest_rectangle_area() function to calculate the maximum area of a rectangle that can be formed using the heights of each column as the heights of the rectangle. The largest_rectangle_area() function uses a stack to keep track of the indices of the columns whose heights have not been used to form a rectangle yet. It then pops the indices from the stack and calculates the maximum area of the rectangle that can be formed using the height of the popped index and the width of the rectangle.

Post a Comment

0Comments
Post a Comment (0)