Skip to content

Instantly share code, notes, and snippets.

@kevinahn7
Last active January 9, 2019 07:03
Show Gist options
  • Select an option

  • Save kevinahn7/204a8b4ad1b69ba5c7a79b8d6180e33f to your computer and use it in GitHub Desktop.

Select an option

Save kevinahn7/204a8b4ad1b69ba5c7a79b8d6180e33f to your computer and use it in GitHub Desktop.
// For a matrix of 0s and you have to find the number of 1s
0001000
0011100
0110000
1110000
0100000
function getRegionSize(matrix, row, column) {
if (row < 0 || column < 0 || row >= matrix.length || column >= matrix[row].length) {
return 0;
} if (matrix[row][column] === 0) {
return 0;
}
matrix[row][column] = 0;
let size = 1;
for (let r = row - 1; r <= row + 1; r++) {
for (let c = column - 1; c <= column + 1; c++) {
if (r !== row || c !== column) {
size += getRegionSize(matrix, r, c);
}
}
}
return size;
}
function getBiggestRegion(matrix) {
let maxRegion = 0;
for (let row = 0; row < matrix.length; row++) {
for (let column = 0; column < matrix[row].length; column++) {
if (matrix[row][column] === 1) {
let size = getRegionSize(matrix, row, column);
if (maxRegion < size) maxRegion = size;
}
}
}
return maxRegion;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment