[InterviewBit] Maximum Sum Square SubMatrix

Maximum Sum Square SubMatrix

  • Time :
  • Space :
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;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/03/PS/interviewbit/maximum-sum-square-submatrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.