All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0200_number_of_islands.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0101_0200.s0200_number_of_islands;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Depth_First_Search
// #Breadth_First_Search #Matrix #Union_Find
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
// #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph
// #Big_O_Time_O(M*N)_Space_O(M*N) #2022_06_28_Time_3_ms_(97.76%)_Space_57.5_MB_(41.23%)

/**
 * 200 - Number of Islands\.
 *
 * Medium
 *
 * 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.
 *
 * **Example 1:**
 *
 * **Input:**
 *
 *     grid = [
 *       ["1","1","1","1","0"],
 *       ["1","1","0","1","0"],
 *       ["1","1","0","0","0"],
 *       ["0","0","0","0","0"]
 *     ]
 *
 * **Output:** 1 
 *
 * **Example 2:**
 *
 * **Input:**
 *
 *     grid = [
 *       ["1","1","0","0","0"],
 *       ["1","1","0","0","0"],
 *       ["0","0","1","0","0"],
 *       ["0","0","0","1","1"]
 *     ]
 *
 * **Output:** 3 
 *
 * **Constraints:**
 *
 * *   `m == grid.length`
 * *   `n == grid[i].length`
 * *   `1 <= m, n <= 300`
 * *   `grid[i][j]` is `'0'` or `'1'`.
**/
public class Solution {
    public int numIslands(char[][] grid) {
        int islands = 0;
        if (grid != null && grid.length != 0 && grid[0].length != 0) {
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (grid[i][j] == '1') {
                        dfs(grid, i, j);
                        islands++;
                    }
                }
            }
        }
        return islands;
    }

    private void dfs(char[][] grid, int x, int y) {
        if (x < 0 || grid.length <= x || y < 0 || grid[0].length <= y || grid[x][y] != '1') {
            return;
        }
        grid[x][y] = 'x';
        dfs(grid, x + 1, y);
        dfs(grid, x - 1, y);
        dfs(grid, x, y + 1);
        dfs(grid, x, y - 1);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy