Yahoo India Web Search

Search results

  1. Feb 23, 2023 · Given a boolean 2D matrix. The task is to find the number of distinct islands where a group of connected 1s (horizontally or vertically) forms an island. Two islands are considered to be distinct if and only if one island is equal to another (not rotated or reflected). Examples: Input: grid[][] = . {{1, 1, 0, 0, 0}, . 1, 1, 0, 0, 0}, .

  2. Given a boolean 2D matrix grid of size n * m. You have to find the number of distinct islands where a group of connected 1s (horizontally or vertically) forms an island. Two islands are considered to be distinct if and only if one island is not equal to another (not rotated or reflected). Example 1: Input: grid[][] = {{1, 1, 0, 0, 0},

  3. Number of Islands - 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.

    • Background
    • Using DFS and Additional Matrix
    • Space Optimizeddfs
    • Using Bfs
    • Using Disjoint Set

    This is a variation of the standard problem: “Counting the number of connected components in an undirected graph”. Let us understand what is a connected component. A connected componentof an undirected graph is a subgraph in which every two vertices are connected to each other by a path(s), and which is connected to no other vertices outside the su...

    Follow the steps below to solve the problem: 1. Initialize count = 0 and boolean matrix, visitedtofalse. 2. For each cell of the input matrix check if the value of the current cell is1and is not visited , call for the DFS for all its 8 neighboring cells. 2.1. If the neighbor is safe to visit and is not visited already Call DFS recursively and Incre...

    Below is the code implementation of the above approach: Time complexity:O(ROW x COL), where ROW is the number of rows and COL is the number of columns in the given matrix. Auxiliary Space: O(ROW * COL), as to do DFS we need extra auxiliary stack space.

    We can solve this problem using BFS as well. The idea is going to be same. We use a visited matrix to keep track if the visited cells and apply the standard queue based BFS algorithm to count islands. We increment the count whenever we see an unvisited vertex after the previous call. The time complexity and auxiliary space are going to be same as D...

    This solution is very intuitive if you have studied disjoint set data structures. We mainly create disjoint sets of all islands and the number of disjoint sets at the end is our answer. Please refer Find the number of Islands | Set 2 (Using Disjoint Set)for details. The time complexity and auxiliary space wise this solution is as good as the BFS ba...

    • 17 min
  4. Oct 24, 2017 · 694. Number of Distinct Islands Description. You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

  5. Mar 10, 2024 · Distinct islands are uniquely shaped groups of connected 1s. This article demonstrates how to compute the number of distinct islands in a 2D binary matrix using Python. For example, given the following matrix: [ [1, 1, 0, 0, 0], [1, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]]

  6. People also ask

  7. 694. Number of Distinct Islands. Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1 's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct islands.