Graph Theory37 sections · 1633 units
Open in Course

LeetCode 463 Island Perimeter - Lessons

Not all grid problems need DFS

Island Perimeter teaches a lesson worth remembering: not every grid problem needs BFS or DFS. When you see a grid, pause before reaching for traversal. Ask yourself: can I solve this with a simple scan?

Perimeter counting works because each land cell contributes independently. You do not need to track connected components or visited cells. The answer is a sum over all cells.

This pattern appears in other problems too: counting neighbors, checking local properties, computing totals. Save traversal for when you need connectivity.