FunctionRacerProblemsRace

Largest Patch

MediumGraphsPython, JavaScript, TypeScript, Java, C++

field is a rectangular grid of integers given as a list of rows; each integer is a crop code. A patch is a maximal group of cells with the same code in which every cell can reach every other by moving up, down, left or right through cells of that same code. Diagonal neighbours do not count.

Return the number of cells in the largest patch. A single cell on its own is a patch of size 1.

Example 1

field [[1,1,2],[1,2,2],[3,3,2]]
returns 4

The four cells holding 2 touch each other in one patch; the 1s form a patch of 3 and the 3s a patch of 2.

Example 2

field [[5]]
returns 1

One cell, one patch.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems