Advertisement
Al3XXX

islands

Feb 3rd, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. class Solution {
  2.     private static final int[] dirY = {0, 0, 1, -1};
  3.     private static final int[] dirX = {1, -1, 0, 0};
  4.     private static final int NOT_VISITED = 1;
  5.     private static final int WATER = 0;
  6.  
  7.     public int largestIsland(int[][] grid) {
  8.         int n = grid.length;
  9.         int maxIslandSize = 0;
  10.         int[] componentSizes = new int[2 + n * n];
  11.         List<Zero> zeroes = new LinkedList<>();
  12.         int nextComponent = 2;
  13.         for (int y = 0; y < n; y++) {
  14.             for (int x = 0; x < n; x++) {
  15.                 if (grid[y][x] == NOT_VISITED) {
  16.                     int component = nextComponent++;                    
  17.                     grid[y][x] = component;                    
  18.                     goDeep(y, x, grid, componentSizes);                    
  19.                     maxIslandSize = Math.max(maxIslandSize, componentSizes[component]);
  20.                 } else if (grid[y][x] == WATER) {
  21.                     zeroes.add(new Zero(y, x));
  22.                 }
  23.             }
  24.         }
  25.  
  26.         Set<Integer> processedComponents = new HashSet<>(6);
  27.         for(Zero zero : zeroes) {
  28.             int newIslandSize = 1;
  29.             for (int dir = 0; dir < 4; dir++) {
  30.                 int y2 = zero.y + dirY[dir];
  31.                 int x2 = zero.x + dirX[dir];
  32.                 if (isOnGrid(y2, x2, grid)) {
  33.                     int component = grid[y2][x2];
  34.                     if (component != WATER && processedComponents.add(component)) {
  35.                         newIslandSize += componentSizes[component];
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             maxIslandSize = Math.max(maxIslandSize, newIslandSize);
  41.             processedComponents.clear();
  42.         }
  43.  
  44.         return maxIslandSize;
  45.     }
  46.  
  47.     private void goDeep(int y, int x, int[][] grid, int[] sizes) {
  48.         int component = grid[y][x];
  49.         sizes[component]++;
  50.         for (int dir = 0; dir < 4; dir++) {
  51.             int y2 = y + dirY[dir];
  52.             int x2 = x + dirX[dir];
  53.             if (isOnGrid(y2, x2, grid) && grid[y2][x2] == NOT_VISITED) {
  54.                 grid[y2][x2] = component;                
  55.                 goDeep(y2, x2, grid, sizes);
  56.             }
  57.         }
  58.     }
  59.  
  60.     private boolean isOnGrid(int y, int x, int[][] grid) {
  61.         return 0 <= y && y < grid.length && 0 <= x && x < grid.length;
  62.     }
  63.  
  64.     private static class Zero {
  65.         int y;
  66.         int x;
  67.  
  68.         public Zero(int y, int x) {
  69.             this.y = y;
  70.             this.x = x;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement