Created
October 12, 2018 05:37
-
-
Save chriskwan/2a0ddb54eb32b63bf9d4be41d6d06161 to your computer and use it in GitHub Desktop.
My solution for https://www.coderbyte.com/language/Maximal%20Square
This file contains 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 MaximalSquare(strArr) { | |
var numRows = strArr.length; | |
var numCols = strArr[0].length; | |
var maxDim = Math.min(numRows, numCols); | |
var dim = maxDim; | |
while (dim > 0) { | |
if (hasSquare(strArr, dim)) { | |
return dim * dim; | |
} | |
dim--; | |
} | |
return 0; | |
} | |
function hasSquare(arr, dim) { | |
for (var row = 0; row < arr.length; row++) { | |
for (var col = 0; col < arr[0].length; col++) { | |
if (hasSquareStartingAt(row, col, arr, dim)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
function hasSquareStartingAt(startingRow, startingCol, arr, dim) { | |
var endRow = startingRow + dim - 1; | |
if (endRow > arr.length-1) { | |
return false; | |
} | |
var endCol = startingCol + dim - 1; | |
if (endCol > arr[0].length-1) { | |
return false; | |
} | |
for (var row = startingRow; row <= endRow; row++) { | |
for (var col = startingCol; col <= endCol; col++) { | |
var value = parseInt(arr[row][col], 10); | |
if (value === 0) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
// keep this function call here | |
MaximalSquare(readline()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment