Problem Summary
Given a N×M rectangular area with some 1×1 area damaged, find the biggest rectangular area without damages.
Solution
I used dynamic programming to solve this problem.
First, let:
height[i][j] be the maximum legal extension to the top of (i,j),
left[i][j] be the maximum legal extension to the left of (i,j),
right[i][j] be the maximum legal extension to the right of (i,j).
“legal extension” means the area from row i-height(i,j)+1 to row i, from column j-left[i][j]+1 to column j+right[i][j]-1 is available.
Then the answer is max{ height[i][j]*(left[i][j]+right[i][j]-1) }, 1<=i<=n,1<=j<=m.
Actually, we do not have to use two-dimensional arrays.
We can scan down row by row, and for each row, we can scan the columns and update the values of height[j] and left[j]. Then we scan the columns backwards and update the values of right[j].
Code
|
|