3
int maxSideLength(vector<vector<int>> &mat, int threshold) {9
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));12
create dp matrix with sum of all squares13
so for the following input matrix19
m = 4; n = 4; least = 4; threshold = 620
you get the dp matrix to be:28
for (int i = 0; i < m; i++) {29
for (int j = 0; j < n; j++) {30
dp[i + 1][j + 1] = mat[i][j] + dp[i + 1][j] + dp[i][j + 1] - dp[i][j];37
from the previous example the once we start looking from least = 4 to see if38
the sum is less than threshold for least = 4; sum = 7 - 0 - 0 + 0. (looking39
at the full 4 x 4 matrix) 7 is > threshold.. so keep looking.. for least =40
3; sum = 5 - 0 - 0 + 0. (looking at the 3 x 3 matrix) .. 5 < threshold..43
say if the threshold was lower e.g. threshold = 2?45
for least = 3; sum = 6 - 3 - 0 + 0 = 3.46
for least = 3; sum = 6 - 0 - 3 + 0 = 3;47
for least = 3; sum = 7 - 4 - 4 + 1 = 0;51
for (int k = least; k > 0; k--) {52
for (int i = k; i < m + 1; i++) {53
for (int j = k; j < n + 1; j++) {54
sum = dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k];55
if (sum <= threshold) {