← Questions

Sizes of connected regions in a binary image, in scan order

0
MicrosoftCodingLast asked Sep 2026

A binary image is an m x n grid of 0s and 1s. Foreground pixels (1s) that touch up, down, left or right belong to the same region; diagonal neighbours do not. Return the size of every region, listed in the order you first meet a pixel of that region when scanning the image row by row, left to right. Discuss BFS versus DFS, recursion depth on large images, and the time and space cost.

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