Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal area matrix covering

Considering an n-by-n binary matrix, I would like to find the minimal area of two rectangles that would cover all the ones (1s). That is, the sum of the areas of the rectangles must be minimal. The rectangles can overlap.

Example:

0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0

The minimal area is: 3 * 9 + 9 * 3 = 54

I'm interested to know if there is a solution better than O(n^4).

like image 412
user1033363 Avatar asked Nov 07 '11 07:11

user1033363


1 Answers

You can first solve the problem for one rectangle by finding the upmost, downmost, rightmost, and leftmost 1s in the matrix. Then you know that you can improve your result by using a second rectangle if and only if you can cut out symmetrical chunks of all 0s.

like image 141
gareth Avatar answered Oct 20 '22 20:10

gareth