Algorithm · Microsoft · Medium
You are given a rectangular grid image made of 0 and 1 values, where 1 marks a foreground pixel and 0 marks a background pixel. Two foreground pixels are part of the same region if one sits directly above, below, to the left of, or to the right of the other. Pixels that touch only at a corner are not connected. Compute the number of foreground pixels in each region and return all of these sizes as an array. The sizes must appear in discovery order: scan the grid row by row…