class Solution {
public int largestIsland(int[][] grid) {
if (grid.length==0) return 0;
Map map = mapIslands(grid);
int max = map.values().stream().reduce(Integer::max).get();
for (int i = 0; i seen = new HashMap<>();
seen.put(validGetColor(grid,i+1,j),map.get(validGetColor(grid,i+1,j)));
seen.put(validGetColor(grid,i-1,j),map.get(validGetColor(grid,i-1,j)));
seen.put(validGetColor(grid,i,j-1),map.get(validGetColor(grid,i,j-1)));
seen.put(validGetColor(grid,i,j+1),map.get(validGetColor(grid,i,j+1)));
max = Math.max(max,seen.values().stream().reduce(0,Integer::sum)+1);
}
}
}
return max;
}
public Map mapIslands(int[][] grid) {
Map map = new HashMap();
int color = 2;
map.put(-1,0);
map.put(0,0);
for (int i = 0; i=grid.length || j>=grid[0].length || i<0 || j<0) return -1;
return grid[i][j];
}
}
Categories