Created
January 14, 2018 20:34
-
-
Save xfateless/e8883ea5e2809c996d9786c77d4033f6 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
var findLeft = function(grid, row, col) { | |
if(col > 0) { | |
return grid[row][col - 1]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findRight = function(grid, row, col) { | |
if(col < grid[row].length-1) { | |
return grid[row][col + 1]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findAbove = function(grid, row, col) { | |
if(row > 0) { | |
return grid[row - 1][col]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findBelow = function(grid, row, col) { | |
if(row < grid.length-1) { | |
return grid[row + 1][col]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findBelowRight = function(grid, row, col) { | |
if(row < grid.length-1 && col < grid[row].length-1) { | |
return grid[row + 1][col + 1]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findBelowLeft = function(grid, row, col) { | |
if(row < grid.length-1 && col > 0) { | |
return grid[row + 1][col - 1]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findAboveRight = function(grid, row, col) { | |
if(row > 0 && col < grid[row].length-1) { | |
return grid[row - 1][col + 1]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findAboveLeft = function(grid, row, col) { | |
if(row > 0 && col > 0) { | |
return grid[row - 1][col - 1]; | |
} else { | |
return undefined; | |
} | |
}; | |
var findIsland = function(grid, row, col, increment) { | |
increment(); | |
grid[row][col] = 0; | |
var left = findLeft(grid, row, col); | |
if(left) { | |
findIsland(grid, row, col - 1, increment); | |
} | |
var right = findRight(grid, row, col); | |
if(right) { | |
findIsland(grid, row, col + 1, increment); | |
} | |
var above = findAbove(grid, row, col); | |
if(above) { | |
findIsland(grid, row - 1, col, increment); | |
} | |
var below = findBelow(grid, row, col); | |
if(below) { | |
findIsland(grid, row + 1, col, increment); | |
} | |
var belowLeft = findBelowLeft(grid, row, col); | |
if(belowLeft) { | |
findIsland(grid, row + 1, col - 1, increment); | |
} | |
var belowRight = findBelowRight(grid, row, col); | |
if(belowRight) { | |
findIsland(grid, row + 1, col + 1, increment); | |
} | |
var aboveLeft = findAboveLeft(grid, row, col); | |
if(aboveLeft) { | |
findIsland(grid, row - 1, col - 1, increment); | |
} | |
var aboveRight = findRight(grid, row, col); | |
if(aboveRight) { | |
findIsland(grid, row - 1, col + 1, increment); | |
} | |
return grid; | |
}; | |
var findMaxIsland = function(grid) { | |
var islands = []; | |
var count = 0; | |
var increment = function() { count++ }; | |
for(var i = 0; i < grid.length; i++) { | |
for(var j = 0; j < grid[i].length; j++) { | |
if(grid[i][j] === 1) { | |
findIsland(grid, i, j, increment); | |
islands.push(count); | |
count = 0; | |
} | |
} | |
} | |
return islands.reduce(function(acc, island) { | |
if(island > acc) { | |
acc = island; | |
} | |
return acc; | |
}, 0); | |
}; | |
var gridExample = [[0, 1, 0, 0], | |
[1, 1, 0, 0], | |
[1, 1, 0, 0], | |
[0, 0, 0, 0]]; | |
findMaxIsland(gridExample); // => 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment