Advertisement
1988coder

200. Number of Islands

Jan 1st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. /**
  2.  * DFS Solution
  3.  *
  4.  * Time Complexity: O(M * N). Visiting all cells of the grid twice.
  5.  *
  6.  * Space Complexity: O(M * N). In worst case if all the cells of the grid are
  7.  * '1', then the recursion stack will be M*N. Also visited array will take M*N
  8.  * space.
  9.  *
  10.  * M = Number of rows in the grid. N = Number of columns in the grid.
  11.  */
  12. class Solution {
  13.     public int numIslands(char[][] grid) {
  14.         if (grid == null || grid.length == 0 || grid[0].length == 0) {
  15.             return 0;
  16.         }
  17.         if (grid.length == 1 && grid[0].length == 1) {
  18.             if (grid[0][0] == '1') {
  19.                 return 1;
  20.             } else {
  21.                 return 0;
  22.             }
  23.         }
  24.  
  25.         int count = 0;
  26.         boolean[][] visited = new boolean[grid.length][grid[0].length];
  27.  
  28.         for (int i = 0; i < grid.length; i++) {
  29.             for (int j = 0; j < grid[0].length; j++) {
  30.                 if (grid[i][j] == '1' && !visited[i][j]) {
  31.                     dfsHelper(grid, i, j, visited);
  32.                     count++;
  33.                 }
  34.             }
  35.         }
  36.         return count;
  37.     }
  38.  
  39.     private void dfsHelper(char[][] grid, int i, int j, boolean[][] visited) {
  40.         if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] != '1' || visited[i][j]) {
  41.             return;
  42.         }
  43.         visited[i][j] = true;
  44.         dfsHelper(grid, i + 1, j, visited);
  45.         dfsHelper(grid, i - 1, j, visited);
  46.         dfsHelper(grid, i, j + 1, visited);
  47.         dfsHelper(grid, i, j - 1, visited);
  48.     }
  49. }
  50.  
  51. /**
  52.  * BFS Solution
  53.  *
  54.  * Time Complexity: O(M * N). Visiting all cells of the grid twice.
  55.  *
  56.  * Space Complexity: O(M * N). Visited array will take M*N space
  57.  *
  58.  * If allowed to modify the grids input array, the space complexity will reduce
  59.  * to O(M + N) or O(min(m,n)). Max size taken by the queue is size of the
  60.  * diagonal of the array.
  61.  *
  62.  * M = Number of rows in the grid. N = Number of columns in the grid.
  63.  */
  64. class Solution {
  65.     private static final int[][] dirs = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
  66.  
  67.     public int numIslands(char[][] grid) {
  68.         if (grid == null || grid.length == 0 || grid[0].length == 0) {
  69.             return 0;
  70.         }
  71.         int rows = grid.length;
  72.         int cols = grid[0].length;
  73.         if (rows == 1 && cols == 1) {
  74.             if (grid[0][0] == '1') {
  75.                 return 1;
  76.             } else {
  77.                 return 0;
  78.             }
  79.         }
  80.  
  81.         int count = 0;
  82.         boolean[][] visited = new boolean[rows][cols];
  83.  
  84.         for (int i = 0; i < rows; i++) {
  85.             for (int j = 0; j < cols; j++) {
  86.                 if (grid[i][j] != '1' || visited[i][j]) {
  87.                     continue;
  88.                 }
  89.  
  90.                 count++;
  91.  
  92.                 Queue<int[]> queue = new LinkedList();
  93.                 queue.offer(new int[] { i, j });
  94.                 visited[i][j] = true;
  95.  
  96.                 while (!queue.isEmpty()) {
  97.                     int[] cell = queue.poll();
  98.                     for (int[] dir : dirs) {
  99.                         int x = cell[0] + dir[0];
  100.                         int y = cell[1] + dir[1];
  101.                         if (x < 0 || y < 0 || x >= rows || y >= cols || grid[x][y] != '1' || visited[x][y]) {
  102.                             continue;
  103.                         }
  104.                         queue.offer(new int[] { x, y });
  105.                         visited[x][y] = true;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         return count;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement