Guest User

Untitled

a guest
Jun 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Solution {
  4.  
  5. static class Node
  6. {
  7. public Node(int x, int y)
  8. {
  9. this.x = x;
  10. this.y = y;
  11. }
  12.  
  13. int x;
  14. int y;
  15. }
  16.  
  17. public int maxAreaOfIsland(int[][] grid) {
  18. int maxRows = grid.length;
  19. int maxCols = grid[0].length;
  20. int maxIslandArea = 0;
  21.  
  22. // Used to check if a 1 has been visited when performing BFS or DFS
  23. boolean[][] isNodeCheckedGrid = new boolean[maxRows][maxCols];
  24.  
  25. // Check for the largest island by iterating through the grid
  26. for (int i = 0; i < maxRows; ++i)
  27. {
  28. for (int j = 0; j < maxCols; ++j)
  29. {
  30. // When we encounter a 1 and that 1 is not yet checked, then perform BFS at that node
  31. if (grid[i][j] == 1 && isNodeCheckedGrid[i][j] == false)
  32. {
  33. int islandArea = countIslandBFS(new Node(i, j), isNodeCheckedGrid, grid);
  34. if (islandArea > maxIslandArea)
  35. {
  36. maxIslandArea = islandArea;
  37. }
  38. }
  39. }
  40. }
  41.  
  42. return maxIslandArea;
  43. }
  44.  
  45. // Perform an iterative BFS to count connected 1s
  46. public static int countIslandBFS(Node startNode, boolean[][] isCheckedGrid, int[][] grid)
  47. {
  48. int islandArea = 0;
  49.  
  50. // Our priorityQ that stores our visited nodes
  51. Deque<Node> bfsQueue = new ArrayDeque<Node>();
  52. bfsQueue.add(startNode);
  53.  
  54. while (bfsQueue.size() != 0)
  55. {
  56. islandArea++;
  57. Node currNode = bfsQueue.pop();
  58. Node leftNode = new Node(currNode.x - 1, currNode.y);
  59. Node rightNode = new Node(currNode.x + 1, currNode.y);
  60. Node topNode = new Node(currNode.x, currNode.y + 1);
  61. Node downNode = new Node(currNode.x, currNode.y - 1);
  62.  
  63. // Check left
  64. if (isWithinGrid(leftNode, isCheckedGrid) && grid[leftNode.x][leftNode.y] == 1)
  65. {
  66. bfsQueue.add(leftNode);
  67. }
  68. // Check right
  69. if (isWithinGrid(rightNode, isCheckedGrid) && grid[rightNode.x][rightNode.y] == 1)
  70. {
  71. bfsQueue.add(rightNode);
  72. }
  73. // Check top
  74. if (isWithinGrid(topNode, isCheckedGrid) && grid[topNode.x][topNode.y] == 1)
  75. {
  76. bfsQueue.add(topNode);
  77. }
  78. // Check down
  79. if (isWithinGrid(downNode, isCheckedGrid) && grid[downNode.x][downNode.y] == 1)
  80. {
  81. bfsQueue.add(downNode);
  82. }
  83. }
  84.  
  85. return islandArea;
  86. }
  87.  
  88. // Simple boolean check to see if a node is within our grid
  89. public static boolean isWithinGrid(Node node, boolean[][] grid)
  90. {
  91. if (node.x >= 0 && node.x < grid.length && node.y >= 0 && node.y < grid[0].length)
  92. {
  93. if (grid[node.x][node.y] == false)
  94. {
  95. grid[node.x][node.y] = true;
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. }
Add Comment
Please, Sign In to add comment