Problem 598233 · easy · Phase 05 Advanced Algorithms & Graphs

Largest Lake

grids · DFS · connected components

A survey map is a grid terrain of 0 (dry land) and 1 (water). A lake is a maximal group of water cells connected through shared sides (diagonal contact does not count). Return the area (number of cells) of the largest lake, or 0 if the map holds no water.

Examples

Input:  terrain = [[0,1,1,0],
                   [0,0,1,0],
                   [1,0,0,0],
                   [1,1,0,1]]
Output: 3
Explanation: lakes {(0,1),(0,2),(1,2)} and {(2,0),(3,0),(3,1)} both have area 3; (3,3) is a lake of area 1.

Input:  terrain = [[0,0],[0,0]]
Output: 0

Constraints

  • 1 <= rows, cols <= 60
  • Target O(rows * cols) time.

Goals

  • Measure the size of each grid component with an iterative search
  • Keep a running maximum across all components
Starting Python…