↓Skip to main content
  1. LeetCode/

LeetCode 695: Max Area of Island

·2 mins· ·
LeetCode Medium Array Depth-First-Search Breadth-First-Search Union-Find
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-30

  • Submissions: Wrong Answer → Accepted

Approach
#

Connected components (BFS Template 2): scan every cell and DFS from each 1, zeroing visited cells in place and counting the island’s size, keeping the maximum. Here cells are the integers 0 and 1, unlike the string "1" in LC 200.

My Attempts
#

Attempt 1 was adapted from LC 200 and still compared with the string '1', while this problem uses integers, so nothing ever matched: [[1]] returned 0. Comparing with 1 passed.

Solution
#

class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        def dfs(i, j):
            if i < 0 or j < 0 or i >= m or j >= n or grid[i][j] != 1:
                return 0
            grid[i][j] = 0
            return 1 + dfs(i + 1, j) + dfs(i - 1, j) + dfs(i, j + 1) + dfs(i, j - 1)
        return max((dfs(i, j) for i in range(m) for j in range(n)), default=0)

Complexity
#

Time O(mn), space O(mn) for recursion depth.

Mistakes to Avoid
#

  • Cells are integers; do not compare with '1'.

Interview Explanation
#

Scanning every cell, I DFS from each 1, zeroing visited cells and counting them, and keep the largest island. Each cell is visited once, so time is O(mn).

Related

LeetCode 417: Pacific Atlantic Water Flow
·3 mins
LeetCode Medium Array Depth-First-Search Breadth-First-Search Matrix
LeetCode 417 note: walk inward from each ocean’s shore to cells no lower than the current one; cells reached from both oceans are the answer
LeetCode 133: Clone Graph
·1 min
LeetCode Medium Hash-Table Depth-First-Search Breadth-First-Search Graph
LeetCode 133 note: during DFS map each old node to its copy and return the existing copy when revisited, avoiding infinite recursion; an empty graph returns None
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 128: Longest Consecutive Sequence
·2 mins
LeetCode Medium Array Hash-Table Union-Find
LeetCode 128 note: put everything in a set and count upward only from values whose x - 1 is absent; each value is seen at most twice, O(n)
LeetCode 79: Word Search
·3 mins
LeetCode Medium Array String Backtracking Depth-First-Search
LeetCode 79 note: backtrack from every cell, matching the next character in four directions with visited cells marked temporarily and restored; prune early with character counts