Largest rectangle of 1's in 2d binary matrix

后端 未结 6 600
野性不改
野性不改 2020-12-07 17:56

There is a problem to find the maximum area of the 1 in the 0-1 matrix. In this problem there are two cases:

  1. area to be measure is of shape square. that\'s

6条回答
  •  暖寄归人
    2020-12-07 18:43

    I would try the following:

    (1) Decompose the matrix into connected components (through BFS).

    (2) For each connected component, look for the maximal rectangle.

    To do (2), I would first look for vertical rectangles: Find the maximal possible width for each consecutive (min_y, max_y), and therefore the area (iteratively, in O(1) per row, just by looking at the min/max of 1's in that row of the connected component). Then I would transpose the matrix, and repeat the process.

    The total running time is O(MxN) for BFS, then O(width^2 + height^2) for each connected componenet, for a total of O(MXN + M^2 + N^2).

    I wonder what's the asymptotically optimal solution though.

提交回复
热议问题