|
| 1 | +/** |
| 2 | + * Finds the size of the largest island that can be formed by changing at most one `0` to `1`. |
| 3 | + * An island is defined as a 4-directionally connected group of `1`s. |
| 4 | + * |
| 5 | + * @param grid - The input n x n binary grid. |
| 6 | + * @returns The size of the largest island after flipping one `0` to `1`. |
| 7 | + */ |
| 8 | +function largestIsland(grid: number[][]): number { |
| 9 | + const n = grid.length; |
| 10 | + |
| 11 | + // Island IDs start from 2 to distinguish from 1s and 0s |
| 12 | + let currentIslandId = 2; |
| 13 | + |
| 14 | + // Maps islandId to its size |
| 15 | + const islandSizes: Record<number, number> = {}; |
| 16 | + |
| 17 | + /** |
| 18 | + * Performs DFS to label the current island and count its size. |
| 19 | + * |
| 20 | + * @param row - The current row index. |
| 21 | + * @param col - The current column index. |
| 22 | + * @param islandId - The ID assigned to the current island. |
| 23 | + */ |
| 24 | + function dfs(row: number, col: number, islandId: number): void { |
| 25 | + // Base case: Out of bounds or not part of the current island |
| 26 | + if (row < 0 || col < 0 || row >= n || col >= n || grid[row][col] !== 1) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + grid[row][col] = islandId; // Label this cell with the current islandId |
| 31 | + islandSizes[islandId]++; // Increment the size of the current island |
| 32 | + |
| 33 | + // Explore all 4 directions |
| 34 | + dfs(row - 1, col, islandId); |
| 35 | + dfs(row + 1, col, islandId); |
| 36 | + dfs(row, col - 1, islandId); |
| 37 | + dfs(row, col + 1, islandId); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * 1. Label all islands and calculate their sizes |
| 42 | + */ |
| 43 | + for (let row = 0; row < n; row++) { |
| 44 | + for (let col = 0; col < n; col++) { |
| 45 | + // Skip water cells and already labeled islands |
| 46 | + if (grid[row][col] !== 1) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + // Initialize the size of the current island |
| 51 | + islandSizes[currentIslandId] = 0; |
| 52 | + |
| 53 | + // Perform DFS to label the current island and count its size |
| 54 | + dfs(row, col, currentIslandId); |
| 55 | + |
| 56 | + // Move to the next island ID |
| 57 | + currentIslandId++; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Calculates the size contributed by neighboring islands when flipping a `0` to `1`. |
| 63 | + * |
| 64 | + * @param row - The row index of the `0` cell. |
| 65 | + * @param col - The column index of the `0` cell. |
| 66 | + * @param visitedIslands - Set to track visited islands and avoid double counting. |
| 67 | + * @returns The size contribution of neighboring islands. |
| 68 | + */ |
| 69 | + function getConnectedIslandSize(row: number, col: number, visitedIslands: Set<number>): number { |
| 70 | + // Out of bounds or water cell or already visited island |
| 71 | + if (row < 0 || col < 0 || row >= n || col >= n || grid[row][col] <= 1) { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + // Get the island ID of the neighboring island |
| 76 | + const islandId = grid[row][col]; |
| 77 | + if (visitedIslands.has(islandId)) { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + visitedIslands.add(islandId); // Mark this island as visited |
| 82 | + return islandSizes[islandId]; // Return its size |
| 83 | + } |
| 84 | + |
| 85 | + let maxIslandSize = 0; |
| 86 | + |
| 87 | + // Flag to check if any 0 was found in the grid |
| 88 | + let haveZeroCell = false; |
| 89 | + |
| 90 | + /** |
| 91 | + * 2. Check each `0` cell to find the maximum possible island size. |
| 92 | + */ |
| 93 | + for (let row = 0; row < n; row++) { |
| 94 | + for (let col = 0; col < n; col++) { |
| 95 | + if (grid[row][col] === 0) { |
| 96 | + // A 0 was found, so flag it |
| 97 | + haveZeroCell = true; |
| 98 | + |
| 99 | + // Track visited neighboring islands |
| 100 | + const visitedIslands = new Set<number>(); |
| 101 | + |
| 102 | + // Calculate the potential island size by flipping this 0 |
| 103 | + let potentialSize = 1; // Start with 1 for the flipped 0 itself |
| 104 | + |
| 105 | + // Check the size of neighboring islands in 4 directions |
| 106 | + potentialSize += getConnectedIslandSize(row - 1, col, visitedIslands); |
| 107 | + potentialSize += getConnectedIslandSize(row + 1, col, visitedIslands); |
| 108 | + potentialSize += getConnectedIslandSize(row, col - 1, visitedIslands); |
| 109 | + potentialSize += getConnectedIslandSize(row, col + 1, visitedIslands); |
| 110 | + |
| 111 | + // Update the maximum island size |
| 112 | + maxIslandSize = Math.max(maxIslandSize, potentialSize); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * 3. Return the maximum island size after flipping one `0` to `1`. |
| 119 | + * If no `0` was found, return the size of the entire grid. |
| 120 | + */ |
| 121 | + return haveZeroCell ? maxIslandSize : n * n; |
| 122 | +} |
0 commit comments