Opening your workspace…
Workspace/Coding room
Opening your workspace…
A pipe has sprung a leak over the tiled cellar floor. Dot is drawing a ferry map for the beetles. On her rectangular grid, 1 marks a dry tile and 0 marks water. Beetles can walk between dry tiles that share an edge. A corner is too far for those little legs. Count each connected patch of dry tiles as one island and return the number of islands. An empty map has zero islands.
[[1,1,0],[0,0,1],[1,0,1]]Output3The two cells at the top left form one island, the cell at the bottom left is a second, and the two cells on the right are a third.
[[1,0],[0,1]]Output2The two land cells only touch at a corner, so they are separate islands.
Scan the grid cell by cell. Each time you reach land you have not visited, count one island and visit all the land connected to it before you carry on scanning.
/** @param {number[][]} grid * @returns {number} */ function solve(grid) { // Explore each island once. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.
[[1,1,0],[0,0,1],[1,0,1]]3