← Questions

Biggest connected component after filling one row or column

0
CognitionCodingLast asked Jul 2026

You get an h by w grid of 0s and 1s. Cells holding 1 are connected when they touch up, down, left or right. You may pick one whole row or one whole column and set every cell in it to 1, or do nothing. Return the size of the largest connected group of 1s you can end up with.

The grid can hold close to a million cells, so trying every row and column with a fresh flood fill is too slow: label the existing components once, then work out how much each row or column would join together.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding