Yahoo India Web Search

Search results

  1. You are given a n,m which means the row and column of the 2D matrix and an array of  size k denoting the number of operations. Matrix elements is 0 if there is water or 1 if there is land. Originally, the 2D matrix is all 0 which means there is .

    • 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
  2. You are supposed to find the number of islands in the grid after each query. An island is a group of lands surrounded by water horizontally, vertically, or diagonally. Detailed explanation ( Input/output format, Notes, Images )

  3. Given a grid of size n*m (n is the number of rows and m is the number of columns in the grid) consisting of '0's (Water) and '1's(Land). Find the number of islands.Note: An island is either surrounded by water or the boundary of a grid and is fo.

  4. Learn how to use Union-Find algorithm to count the number of islands after adding land to a 2D grid. See problem description, solution approach, example walkthrough and code implementation.

  5. Sep 30, 2016 · Learn how to solve the problem of adding land to a 2D binary grid and counting the number of islands. See the solution using union-find algorithm and the time complexity analysis.

  6. 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.