Knowee
Questions
Features
Study Tools

200. Number of IslandsMediumTopicsCompaniesGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Question

  1. Number of IslandsMediumTopicsCompaniesGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step solution in Python for the problem:

def numIslands(grid):
    if not grid:
        return 0
        
    count = 0
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            if grid[i][j] == '1':
                dfs(grid, i, j)
                count += 1
    return count

def dfs(grid, i, j):
    if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != '1':
        return
    grid[i][j] = '#'
    dfs(grid, i+1, j)
    dfs(grid, i-1, j)
    dfs(grid, i, j+1)
    dfs(grid, i, j-1)

Here's how it works:

  1. We start by checking if the grid is empty. If it is, we return 0 because there are no islands.

  2. We then iterate over each cell in the grid. If a cell contains a '1', we increment our island count and call our depth-first search (dfs) function on that cell.

  3. The dfs function is used to mark the visited '1's in the current island. If the current cell is out of bounds or is not a '1', we return. Otherwise, we mark the current cell as visited by setting its value to '#', and then recursively call dfs on its neighboring cells.

  4. Finally, we return the count of islands.

This problem has been solved

Similar Questions

Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

200. Number of Islands

Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.Return the number of closed islands.Input FormatThe First line contains two integers denoting the value of n and mThe Next N lines contains M integers denoting the elements of the matrix.Constraints1 <= grid.length, grid[0].length <= 1000 <= grid[i][j] <=1Output FormatPrint a integer which is the answer to the question.Sample Input 03 50 0 1 0 00 1 0 1 00 1 1 1 0Sample Output 01Sample Input 13 41 0 1 1 1 1 1 1 1 1 1 1 Sample Output 10

You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

According to island biogeography, what is the relationship between an island's distance from the mainland and the number of species present on the island?

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.