The question is, given a matrix with all cells black or white, to find the maximum sized submatrix with all border cells black (the interior cells need not be black).
Currently I can at best think of an O(N^4)
solution. For this, I make 2 auxiliary tables, one with count of consecutive black cells to the right in each row for each cell, and in the other, count of consecutive black cells in that column for each cell. Then I do it as follows:
for each row (i):
for each cell (i,j):
for each window (1..n-j):
if auxrow[i,j+window]-auxrow[i,j] == window: #so, all cells in this window is black
colsleft = auxcol[i,j]
colsright = auxcol[i,j+window]
botttom_row = min(colsleft,colsright)
for bot in (row..row+mincol):
if auxrow[bot][j+window]-auxrow[bot][j] == window:
maxlen = ... #do whatever to save this sub-matrix as answer
How to improve this solution? I saw some interesting discussion at Topcoder, particularly the answer by Rem (O(N^2*log N)
), and the follow-up improvement suggested by Tomek, but I could understand neither solution! Can someone give a solution better than mine, or explain those algorithms?