Max Area of Island - Complete Solution Guide
Max Area of Island is LeetCode problem 695, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. The area of an island is the number of cells with a value 1 in the island. Return the maximum area of an island in grid . If there is no island, return 0 . Example 1: 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
Detailed Explanation
The problem asks us to find the maximum area of an island in a given binary matrix (grid). An island is defined as a group of '1's that are connected 4-directionally (up, down, left, right). The input is a 2D array (grid) of 0s and 1s, where 1 represents land and 0 represents water. The output should be the largest number of connected 1s in the grid. If there are no islands (no 1s), the output should be 0.
Solution Approach
The solution uses Depth-First Search (DFS) to explore each island and calculate its area. The algorithm iterates through each cell of the grid. If a cell contains a '1', it means a new island is encountered. A DFS is initiated from that cell to explore the entire island. During the DFS, each visited '1' is changed to '0' to mark it as visited and prevent revisiting. The area of the island is calculated during the traversal. The maximum area found so far is updated.
Step-by-Step Algorithm
- Step 1: Initialize a variable `max_area` to 0 to store the maximum island area found so far.
- Step 2: Iterate through each cell in the `grid` using nested loops.
- Step 3: If the current cell `grid[i][j]` is equal to 1, then it represents a starting point of an island.
- Step 4: Initialize `current_area` to 0 and create a stack to perform DFS. Push the starting cell (i, j) onto the stack.
- Step 5: Change the value of the current cell `grid[i][j]` to 0 to mark it as visited. This is crucial to prevent infinite loops.
- Step 6: While the stack is not empty:
- a. Pop a cell (r, c) from the stack.
- b. Increment `current_area` by 1.
- c. Explore the 4-directional neighbors (up, down, left, right) of the current cell (r, c).
- d. For each neighbor (nr, nc), check if it is within the grid boundaries and if its value is 1 (unvisited land).
- e. If the neighbor is a valid unvisited land, push it onto the stack and mark it as visited by setting `grid[nr][nc]` to 0.
- Step 7: After the DFS is complete for the current island, update `max_area` to be the maximum of `max_area` and `current_area`.
- Step 8: After iterating through all the cells in the `grid`, return `max_area`.
Key Insights
- Insight 1: The problem can be solved using Depth-First Search (DFS) or Breadth-First Search (BFS) to explore each island.
- Insight 2: To avoid infinite loops and double-counting, we need to mark visited cells (cells with value 1) as visited by changing their value to 0 after encountering them.
- Insight 3: Iterate through the grid, and whenever you find a '1', start a DFS/BFS to explore the island connected to that cell. Keep track of the largest area found so far.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(m*n)
Topics
This problem involves: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix.
Companies
Asked at: Disney, DoorDash, Dropbox, Grubhub, Intuit, LinkedIn, Salesforce, Smartsheet, Snowflake, Tesla, Wise.