Skip to content

Instantly share code, notes, and snippets.

@syphh
Last active December 22, 2019 21:44
Show Gist options
  • Select an option

  • Save syphh/fb89851e92cab08d5dd5dade6ebd3f1f to your computer and use it in GitHub Desktop.

Select an option

Save syphh/fb89851e92cab08d5dd5dade6ebd3f1f to your computer and use it in GitHub Desktop.
Sum of sub-matrices (Brute force method)
function sumSubmatrices(mat){
// n and m represent dimensions of the matrix
let n = mat.length;
let m = mat[0].length;
// declaring a (n*m) matrix
let sumMat = [...new Array(n)].map(row => [...new Array(m)]);
for(let i = 0; i < n; i++){
for(let j = 0; j < m; j++){
sumMat[i][j] = 0; // initializing the cell to 0
for(let ii = 0; ii <= i; ii++){
for(let jj = 0; jj <= j; jj++){
sumMat[i][j] += mat[ii][jj];
}
}
}
}
return sumMat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment