Generation

generate functionSun, 20 Nov 2022

implement a two dimensional array filled with zeroes and ones. Ones in the two dimensional array makes islands. Find the largest island in the two dimensional array

class Solution { public int largestIsland(int[][] grid) { // create a map to store the index and area Map<Integer, Integer> map = new HashMap<>(); int index = 2; int max = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] == 1) { int area = dfs(grid, i, j, index); map.put(index, area); max = Math.max(max, area); index++; } } } // if all ones, return the area if (index == 2) return grid.length * grid[0].length; // loop through each zero element and find largest area by merging islands for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (

Want to kickstart your project?Use the new AI Studio to create your code