Max Rectangle In Binary Matrix

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 161:-Max Rectangle In Binary Matrix

The problem of finding the maximum rectangle in a binary matrix is a common problem that can be solved using dynamic programming and the concept of the largest rectangle in a histogram.

Here's how you can approach this problem:

  1. Convert each row of the binary matrix into a histogram. Treat the matrix as a series of histograms stacked on top of each other, where each histogram represents the heights of consecutive 1s in the binary matrix.

  2. For each row, calculate the histogram heights using dynamic programming. The height of the histogram at each position is determined by the number of consecutive 1s above it. This can be done by iterating through the row and updating the heights accordingly.

  3. For each histogram, calculate the largest rectangle using the "largest rectangle in a histogram" algorithm. This algorithm maintains a stack to keep track of the increasing heights of the histogram bars. When a smaller height is encountered, the algorithm pops elements from the stack and calculates the area of the rectangles that can be formed with the popped heights as the minimum height.

  4. Keep track of the maximum area encountered while iterating through each histogram.

Here's a C++ implementation of finding the maximum rectangle in a binary matrix:

 

cppCopy code

#include <iostream> #include <vector> #include <stack> #include <algorithm> int maximalRectangle(const std::vector<std::vector<int>>& matrix) { if (matrix.empty()) return 0; int rows = matrix.size(); int cols = matrix[0].size(); int maxArea = 0; std::vector<int> heights(cols, 0); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (matrix[i][j] == 0) { heights[j] = 0; } else { heights[j] += 1; } } std::stack<int> stack; for (int j = 0; j <= cols; ++j) { while (!stack.empty() && (j == cols || heights[j] < heights[stack.top()])) { int height = heights[stack.top()]; stack.pop(); int width = stack.empty() ? j : j - stack.top() - 1; maxArea = std::max(maxArea, height * width); } stack.push(j); } } return maxArea; } int main() { std::vector<std::vector<int>> matrix = { {1, 0, 1, 0, 0}, {1, 0, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 0, 0, 1, 0} }; int maxRectangle = maximalRectangle(matrix); std::cout << "Maximum Rectangle Area: " << maxRectangle << std::endl; return 0; }

In this example, the maximalRectangle function calculates the maximum rectangle area in the given binary matrix using dynamic programming and the largest rectangle in a histogram algorithm. The main function demonstrates how to use this function for a specific input matrix.

For the given binary matrix, the output will be:

 

mathematicaCopy code

Maximum Rectangle Area: 6

This indicates that the maximum rectangle area in the binary matrix is 6.

21. Stacks - Assignments

2 Comments

@mk.info.work
mk.info.work Feb 17, 2024 at 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

@sciaku1
sciaku1 Jan 11, 2024 at 3:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support