#695

Max Area of Island

Medium DFS returning size

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

In a 0/1 grid, return the area (number of cells) of the largest island, or 0.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the size of the biggest island.

Why this works: same as counting islands, but the DFS returns how many cells it sank.

Example: an island of 4 cells and one of 6

answer 6

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Not marking cells as visited before recursing, so cells are counted multiple times.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n).
  • Space: O(m·n).
Topic: Graphs
🎬 Episodes 74–85: video coming soon