Last active
December 22, 2019 21:44
-
-
Save syphh/fb89851e92cab08d5dd5dade6ebd3f1f to your computer and use it in GitHub Desktop.
Sum of sub-matrices (Brute force method)
This file contains hidden or 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
| 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