Why it works
- The area of the land reachable from a cell is 1 for the cell plus the areas reachable from its land neighbours, so each call adds up what its calls return.
- A cell becomes water as soon as a call enters it, so no other call counts it twice, even where branches of an island meet.
- Each island is measured whole by one call from the scan, so comparing those results with best finds the biggest.
Complexity
Time O(rows × cols): each cell is scanned once and each land cell is entered once. Memory O(rows × cols) at worst, for a call stack as deep as one long island.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗