|
| 1 | +--- |
| 2 | +id: 695-max-area-of-island |
| 3 | +title: Max Area of Island |
| 4 | +sidebar_label: 0695 - Max Area of Island |
| 5 | +tags: |
| 6 | + - Depth-First Search |
| 7 | + - Recursion |
| 8 | + - Breadth-First Search |
| 9 | +description: "This is a solution to the Max Area of Island problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +You are given an `m x n` binary matrix `grid`. An island is a group of `1`'s (representing land) connected **4-directionally** (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. |
| 15 | + |
| 16 | +The **area** of an island is the number of cells with a value `1` in the island. |
| 17 | + |
| 18 | +Return the maximum **area** of an island in `grid`. If there is no island, return `0`. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | + |
| 25 | +``` |
| 26 | +Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] |
| 27 | +Output: 6 |
| 28 | +Explanation: The answer is not 11, because the island must be connected 4-directionally. |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: grid = [[0,0,0,0,0,0,0,0]] |
| 35 | +Output: 0 |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `m == grid.length` |
| 41 | +- `n == grid[i].length` |
| 42 | +- `1 <= m, n <= 50` |
| 43 | +- `grid[i][j]` is either 0 or 1. |
| 44 | + |
| 45 | +## Solution for Max Area of Island |
| 46 | + |
| 47 | +### Approach 1: Depth-First Search (Recursive) |
| 48 | +#### Intuition and Algorithm |
| 49 | + |
| 50 | +We want to know the area of each connected shape in the grid, then take the maximum of these. |
| 51 | + |
| 52 | +If we are on a land square and explore every square connected to it 4-directionally (and recursively squares connected to those squares, and so on), then the total number of squares explored will be the area of that connected shape. |
| 53 | + |
| 54 | +To ensure we don't count squares in a shape more than once, let's use `seen` to keep track of squares we haven't visited before. It will also prevent us from counting the same shape more than once. |
| 55 | +## Code in Different Languages |
| 56 | + |
| 57 | +<Tabs> |
| 58 | +<TabItem value="java" label="Java"> |
| 59 | + <SolutionAuthor name="@Shreyash3087"/> |
| 60 | + |
| 61 | +```java |
| 62 | +class Solution { |
| 63 | + int[][] grid; |
| 64 | + boolean[][] seen; |
| 65 | + |
| 66 | + public int area(int r, int c) { |
| 67 | + if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || |
| 68 | + seen[r][c] || grid[r][c] == 0) |
| 69 | + return 0; |
| 70 | + seen[r][c] = true; |
| 71 | + return (1 + area(r+1, c) + area(r-1, c) |
| 72 | + + area(r, c-1) + area(r, c+1)); |
| 73 | + } |
| 74 | + |
| 75 | + public int maxAreaOfIsland(int[][] grid) { |
| 76 | + this.grid = grid; |
| 77 | + seen = new boolean[grid.length][grid[0].length]; |
| 78 | + int ans = 0; |
| 79 | + for (int r = 0; r < grid.length; r++) { |
| 80 | + for (int c = 0; c < grid[0].length; c++) { |
| 81 | + ans = Math.max(ans, area(r, c)); |
| 82 | + } |
| 83 | + } |
| 84 | + return ans; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +</TabItem> |
| 90 | +<TabItem value="python" label="Python"> |
| 91 | + <SolutionAuthor name="@Shreyash3087"/> |
| 92 | + |
| 93 | +```python |
| 94 | +class Solution(object): |
| 95 | + def maxAreaOfIsland(self, grid): |
| 96 | + seen = set() |
| 97 | + def area(r, c): |
| 98 | + if not (0 <= r < len(grid) and 0 <= c < len(grid[0]) |
| 99 | + and (r, c) not in seen and grid[r][c]): |
| 100 | + return 0 |
| 101 | + seen.add((r, c)) |
| 102 | + return (1 + area(r+1, c) + area(r-1, c) + |
| 103 | + area(r, c-1) + area(r, c+1)) |
| 104 | + |
| 105 | + return max(area(r, c) |
| 106 | + for r in range(len(grid)) |
| 107 | + for c in range(len(grid[0]))) |
| 108 | +``` |
| 109 | +</TabItem> |
| 110 | +</Tabs> |
| 111 | + |
| 112 | +## Complexity Analysis |
| 113 | + |
| 114 | +### Time Complexity: $O(R \times C)$ |
| 115 | + |
| 116 | +> **Reason**: where R is the number of rows in the given grid, and C is the number of columns. We visit every square once. |
| 117 | +
|
| 118 | +### Space Complexity: $O(R \times C)$ |
| 119 | + |
| 120 | +> **Reason**: the space used by `seen` to keep track of visited squares and the space used by the call stack during our recursion. |
| 121 | +
|
| 122 | +### Approach 2: Depth-First Search (Iterative) |
| 123 | +#### Intuition and Algorithm |
| 124 | + |
| 125 | +We can try the same approach using a stack-based, (or "iterative") depth-first search. |
| 126 | + |
| 127 | +Here, `seen` will represent squares that have either been visited or are added to our list of squares to visit (`stack`). For every starting land square that hasn't been visited, we will explore 4-directionally around it, adding land squares that haven't been added to `seen` to our `stack`. |
| 128 | + |
| 129 | +On the side, we'll keep a count `shape` of the total number of squares seen during the exploration of this shape. We'll want the running max of these counts. |
| 130 | + |
| 131 | +## Code in Different Languages |
| 132 | + |
| 133 | +<Tabs> |
| 134 | +<TabItem value="java" label="Java"> |
| 135 | + <SolutionAuthor name="@Shreyash3087"/> |
| 136 | + |
| 137 | +```java |
| 138 | +class Solution { |
| 139 | + public int maxAreaOfIsland(int[][] grid) { |
| 140 | + boolean[][] seen = new boolean[grid.length][grid[0].length]; |
| 141 | + int[] dr = new int[]{1, -1, 0, 0}; |
| 142 | + int[] dc = new int[]{0, 0, 1, -1}; |
| 143 | + |
| 144 | + int ans = 0; |
| 145 | + for (int r0 = 0; r0 < grid.length; r0++) { |
| 146 | + for (int c0 = 0; c0 < grid[0].length; c0++) { |
| 147 | + if (grid[r0][c0] == 1 && !seen[r0][c0]) { |
| 148 | + int shape = 0; |
| 149 | + Stack<int[]> stack = new Stack(); |
| 150 | + stack.push(new int[]{r0, c0}); |
| 151 | + seen[r0][c0] = true; |
| 152 | + while (!stack.empty()) { |
| 153 | + int[] node = stack.pop(); |
| 154 | + int r = node[0], c = node[1]; |
| 155 | + shape++; |
| 156 | + for (int k = 0; k < 4; k++) { |
| 157 | + int nr = r + dr[k]; |
| 158 | + int nc = c + dc[k]; |
| 159 | + if (0 <= nr && nr < grid.length && |
| 160 | + 0 <= nc && nc < grid[0].length && |
| 161 | + grid[nr][nc] == 1 && !seen[nr][nc]) { |
| 162 | + stack.push(new int[]{nr, nc}); |
| 163 | + seen[nr][nc] = true; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + ans = Math.max(ans, shape); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + return ans; |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +</TabItem> |
| 177 | +<TabItem value="python" label="Python"> |
| 178 | + <SolutionAuthor name="@Shreyash3087"/> |
| 179 | + |
| 180 | +```python |
| 181 | +class Solution(object): |
| 182 | + def maxAreaOfIsland(self, grid): |
| 183 | + seen = set() |
| 184 | + ans = 0 |
| 185 | + for r0, row in enumerate(grid): |
| 186 | + for c0, val in enumerate(row): |
| 187 | + if val and (r0, c0) not in seen: |
| 188 | + shape = 0 |
| 189 | + stack = [(r0, c0)] |
| 190 | + seen.add((r0, c0)) |
| 191 | + while stack: |
| 192 | + r, c = stack.pop() |
| 193 | + shape += 1 |
| 194 | + for nr, nc in ((r-1, c), (r+1, c), (r, c-1), (r, c+1)): |
| 195 | + if (0 <= nr < len(grid) and 0 <= nc < len(grid[0]) |
| 196 | + and grid[nr][nc] and (nr, nc) not in seen): |
| 197 | + stack.append((nr, nc)) |
| 198 | + seen.add((nr, nc)) |
| 199 | + ans = max(ans, shape) |
| 200 | + return ans |
| 201 | +``` |
| 202 | +</TabItem> |
| 203 | +</Tabs> |
| 204 | + |
| 205 | +## Complexity Analysis |
| 206 | + |
| 207 | +### Time Complexity: $O(R \times C)$ |
| 208 | + |
| 209 | +> **Reason**: where R is the number of rows in the given grid, and C is the number of columns. We visit every square once. |
| 210 | +
|
| 211 | +### Space Complexity: $O(R \times C)$ |
| 212 | + |
| 213 | +> **Reason**: the space used by seen to keep track of visited squares and the space used by stack. |
| 214 | +
|
| 215 | +## References |
| 216 | + |
| 217 | +- **LeetCode Problem**: [Max Area of Island](https://leetcode.com/problems/max-area-of-island/description/) |
| 218 | + |
| 219 | +- **Solution Link**: [Max Area of Island](https://leetcode.com/problems/max-area-of-island/solutions/) |
0 commit comments