Maximum Sum Square SubMatrix
1 2 3 4 5 6 7 8 9 10 11 12 13
| int Solution::solve(vector<vector<int> > &A, int B) { int n = A.size(); vector<vector<int>> dp(n + 1, vector<int>(n + 1)); int res = INT_MIN; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] - dp[i][j] + A[i][j]; if(i + 1>= B and j + 1>= B) res = max(res, dp[i+1][j+1] - dp[i+1-B][j+1] - dp[i+1][j+1-B] + dp[i+1-B][j+1-B]); } } return res; }
|