The Matrix Maximum Rectangle Area

The Matrix Maximum Rectangle Area

Given a binary matrix, calculate maximum size rectangle area in a binary-sub-matrix with all 1’s. Your function should return an integer denoting the area of the maximum rectangle.

Example:

0 1 1 0
1 1 1 1
1 1 1 1
1 1 0 0
the max size rectangle is
1 1 1 1
1 1 1 1
Area is 4 * 2 = 8

Solution:
Problem become simple when we use Largest Rectangle.

Algorithm:
  • Take an array temp of same size of matrix.
  • Set MaxArea = 0
  • Iterate matrix row (i) from 0 to n – 1
    • Iterate matrix each col (j) for ith row
      • If matrix[i][j] > 0 then
        • temp[j] += matrix[i][j]
      • Else
        • temp[j] = 0
    • calculate area = LargestRectangle.calculate(temp)
    • If MaxArea < area then
      • Set MaxArea = area
  • Return MaxArea

Matrix Maximum Rectangle Area

Latest Source Code:
Github: MaxRectangle.java


Output:

9
Author: Hrishikesh Mishra