Advertisement
1988coder

695. Max Area of Island

Jan 1st, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. /**
  2.  * DFS Recursive Solution.
  3.  *
  4.  * Time Complexity: O(M * N) - All cells in the grids ar visited twice.
  5.  *
  6.  * Space Complexity: O(M * N) - This is for recursion stack and visited boolean
  7.  * array
  8.  *
  9.  * M = Number of rows in the grid. N = Number of columns in the grid.
  10.  */
  11. class Solution {
  12.     public int maxAreaOfIsland(int[][] grid) {
  13.         if (grid == null || grid.length == 0 || grid[0].length == 0) {
  14.             return 0;
  15.         }
  16.         if (grid.length == 1 && grid[0].length == 1) {
  17.             if (grid[0][0] == 1) {
  18.                 return 1;
  19.             } else {
  20.                 return 0;
  21.             }
  22.         }
  23.  
  24.         boolean[][] visited = new boolean[grid.length][grid[0].length];
  25.         int maxArea = 0;
  26.  
  27.         for (int i = 0; i < grid.length; i++) {
  28.             for (int j = 0; j < grid[0].length; j++) {
  29.                 if (grid[i][j] == 1 && !visited[i][j]) {
  30.                     int area = dfsHelper(grid, i, j, visited);
  31.                     maxArea = Math.max(maxArea, area);
  32.                 }
  33.             }
  34.         }
  35.  
  36.         return maxArea;
  37.     }
  38.  
  39.     private int dfsHelper(int[][] 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 0;
  42.         }
  43.         visited[i][j] = true;
  44.         return 1 + dfsHelper(grid, i + 1, j, visited) + dfsHelper(grid, i - 1, j, visited)
  45.                 + dfsHelper(grid, i, j + 1, visited) + dfsHelper(grid, i, j - 1, visited);
  46.     }
  47. }
  48.  
  49. /**
  50.  * DFS Iterative Solution.
  51.  *
  52.  * Time Complexity: O(M * N) - All cells in the grids ar visited twice.
  53.  *
  54.  * Space Complexity: O(M * N) - Visited boolean array. Max size taken by the
  55.  * stack is O(min(M,N)).
  56.  *
  57.  * M = Number of rows in the grid. N = Number of columns in the grid.
  58.  */
  59. class Solution {
  60.     public int maxAreaOfIsland(int[][] grid) {
  61.         if (grid == null || grid.length == 0 || grid[0].length == 0) {
  62.             return 0;
  63.         }
  64.         int rows = grid.length;
  65.         int cols = grid[0].length;
  66.         if (rows == 1 && cols == 1) {
  67.             if (grid[0][0] == 1) {
  68.                 return 1;
  69.             } else {
  70.                 return 0;
  71.             }
  72.         }
  73.  
  74.         int maxArea = 0;
  75.         boolean[][] visited = new boolean[rows][cols];
  76.         int[][] dirs = new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
  77.  
  78.         for (int i = 0; i < rows; i++) {
  79.             for (int j = 0; j < cols; j++) {
  80.                 if (grid[i][j] != 1 || visited[i][j]) {
  81.                     continue;
  82.                 }
  83.  
  84.                 Stack<int[]> stack = new Stack();
  85.                 stack.push(new int[] { i, j });
  86.                 visited[i][j] = true;
  87.                 int area = 1;
  88.                 while (!stack.isEmpty()) {
  89.                     int[] cell = stack.pop();
  90.                     for (int[] dir : dirs) {
  91.                         int x = cell[0] + dir[0];
  92.                         int y = cell[1] + dir[1];
  93.                         if (x < 0 || y < 0 || x >= rows || y >= cols || grid[x][y] != 1 || visited[x][y]) {
  94.                             continue;
  95.                         }
  96.                         visited[x][y] = true;
  97.                         area++;
  98.                         stack.push(new int[] { x, y });
  99.                     }
  100.                 }
  101.  
  102.                 maxArea = Math.max(maxArea, area);
  103.             }
  104.         }
  105.  
  106.         return maxArea;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement