Skip to content

Instantly share code, notes, and snippets.

@RaneWallin
Created August 5, 2020 02:18
Show Gist options
  • Save RaneWallin/d0a6e1088bf1a05871f11efd1eb932e7 to your computer and use it in GitHub Desktop.
Save RaneWallin/d0a6e1088bf1a05871f11efd1eb932e7 to your computer and use it in GitHub Desktop.
// edge cases: no islands, all zeros
// constraints: none
// start with first row
// walk through each index in the row
// if it sees a 1
// check each potential island in array
// check each coordinate
// determine if the stored coordinate is adjacent to the current coordinate
// if so add that coordinate to that array
// check if there are any matches in the matching array
// if there are, join the arrays
// create a new array with all coordinates from both arrays
// slice out the other matching array
// add new array index to matches
// otherwise add index to array of matches for this coordinate
// otherwise add it to a new potential island array
// continue until all rows are checked
// return the size of the final array
/*
Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment