Minimum Island

Minimum Island

A grid contains land (L) and water (W). Return the size (number of cells) of the smallest island. Assume there is at least one island.

minimumIsland(grid)

Input format: rows separated by |.

Examples

LLWW|WLWW|WWWL   // → 1   (islands of size 3 and 1)
LLWWW|WWWLL|WWWLW // → 2
L                 // → 1

Walkthrough

Reusing the island-counting machinery

This uses the exact same idea as counting islands: treat the grid as an implicit graph, scan every cell, and flood-fill each unvisited island while marking its cells visited. The only change is what you do with each island once found — instead of just counting it, you need its size.

Measuring size during the flood-fill

The flood-fill needs to report a count of cells touched, not just mark and return nothing. Visiting one land cell contributes 1, plus whatever sizes come back from recursively flood-filling each unvisited land neighbour. A neighbour out of bounds, water, or already visited contributes 0.

Tracking the minimum

As you discover each island, compare its size against the smallest seen so far. A common pitfall: initializing the running minimum to 0 — since 0 is smaller than every real island, it never gets replaced, and you'd wrongly report 0. Instead, initialize it from the first island's size, or use a sentinel meaning "nothing found yet" and check for it explicitly. Same class of bug as seeding a running maximum with 0 in an all-negative array.

Why the visited set still matters

Land cells can form loops, so without a visited set the flood-fill could revisit cells repeatedly, both running forever and inflating the reported size. Marking a cell visited the instant it's counted guarantees it contributes to exactly one island's size, exactly once.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.