Created
September 6, 2018 04:30
-
-
Save vinicius5581/f594988319cb3ef356105ebe2ccd9a27 to your computer and use it in GitHub Desktop.
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
const matrix = [ | |
[1,1,1,0,1], | |
[0,1,1,1,0], | |
[1,1,1,1,1], | |
[0,1,1,1,1], | |
[1,1,1,0,1] | |
]; | |
function findLargestSquareOf1s(matrix) { | |
let max = 0; | |
const matrixCache = []; | |
for (let i = 0; i < matrix.length; i++) { | |
matrixCache[i] = []; | |
for (let j = 0; j < matrix[0].length; j++) { | |
matrixCache[i][j] = null; | |
} | |
} | |
for(let i = 0; i < matrix.length; i++){ | |
for (let j = 0; j < matrix[0].length; j++) { | |
// console.log(i + ',' + j); | |
if (matrix[i][j] !== 0) { | |
if (i === 0 || j === 0 ) { | |
matrixCache[i][j] = matrix[i][j]; | |
} else { | |
const top = matrixCache[i-1][j]; | |
const topLeft = matrixCache[i-1][j-1]; | |
const left = matrixCache[1][j-1]; | |
const neighboorsMin = Math.min(top, topLeft, left); | |
matrixCache[i][j] = neighboorsMin + matrix[i][j]; | |
} | |
if (matrixCache[i][j] > max) { | |
max = matrixCache[i][j]; | |
} | |
} | |
} | |
} | |
console.log(matrixCache); | |
return max; | |
} | |
console.log(findLargestSquareOf1s(matrix)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment