← Discussions on Number of Islands
S srinivas_18
12 Jun 2026, 18:37

Asked in my Adobe OA last week.

A single pass with a running value is enough here. Keep track of the best answer so far and update it as you scan, which gives O(n) time and O(1) extra space.

3 replies

kavyapat8 4 months ago

Thanks! The dry run on the second example made it click.

sravyagoud 4 months ago

Thanks! The dry run on the second example made it click.

anirudhchatterjee 3 months, 4 weeks ago

For the TLE: you are rebuilding the list inside the loop, move it outside.

Sign in to reply.