Data Structures & Algorithms Graphs — BFS & DFS
💡
Exercise 111

Max Area of Island 20 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given a 2D grid of 0s and 1s, find the maximum area of an island. The area = number of connected 1-cells.

grid = [ [0,0,1,0,0], [0,0,1,1,0], [0,1,1,0,0] ] # Max area = 5

Same DFS flood-fill as Number of Islands, but count cells during each DFS and track the maximum.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find the maximum area of an island in the grid. Return 0 if no islands.
DFS returns area: 1 + sum of recursive calls for 4 directions. Track max_area across all cells.
🧪 Test Cases
Input
maxAreaOfIsland(grid)
Expected
5
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.