Last active
December 20, 2015 07:59
-
-
Save toliuweijing/6097144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 0th O(n) on all candidate bottoms @B. | |
// 1th O(n) on candidate tops @T. | |
// 2th O(n) on finding the maximum @left/@right match. | |
int maxRect(vector<vector<int> >& mat) { | |
int n = mat.size(); | |
vector<vector<int> >& colSum = mat; | |
for (int i = 1 ; i < n ; ++i) | |
for (int j = 0 ; j < n ; ++j) | |
colSum[i][j] += colSum[i-1][j]; | |
int optrect = 0; | |
for (int b = 0 ; b < n ; ++b) { | |
for (int t = 0 ; t <= b ; ++t) { | |
int minLeft = 0; | |
int rowSum[n]; | |
for (int i = 0 ; i < n ; ++i) { | |
int col = t == 0 ? colSum[b][i] : colSum[b][i] - colSum[t-1][i]; | |
rowSum[i] = i == 0? col : col + rowSum[i-1]; | |
optrect = max(optrect, rowSum[i] - minLeft); | |
minLeft = min(minLeft, rowSum[i]); | |
} | |
} | |
} | |
return optrect; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment