FastPrepFlip, Invert, and Smooth a Binary Image

Flip, Invert, and Smooth a Binary Image

Abridge logoAbridge● MediumFULLTIMEOA
Learn

Problem statement

Given a rectangular binary matrix image, perform two transformations without modifying the input.

  1. Flip every row horizontally, then invert each bit.
  2. For each cell of that transformed matrix, compute the floor of the average of the cell and all valid neighbors in its surrounding 3 x 3 region.

Return the smoothed matrix. Every smoothing average must read from the complete flipped-and-inverted matrix, not from partially written output.

Function

flipInvertAndSmooth(image: int[][]) → int[][]

Examples

Example 1

image = [[0,0,0],[0,0,0],[1,0,0]]return = [[1,1,1],[1,0,0],[1,0,0]]

Flipping and inverting produces [[1,1,1],[1,1,1],[1,1,0]]. Each result cell is the floored neighborhood average of that complete intermediate matrix.

Example 2

image = [[0,0],[0,0]]return = [[1,1],[1,1]]

The first transformation produces all ones, and every valid neighborhood therefore averages to one.

Example 3

image = [[1]]return = [[0]]

The single bit becomes zero; its one-cell neighborhood also averages to zero.

Constraints

  • 1 <= image.length, image[i].length <= 200.
  • Every row has the same length.
  • Every cell is 0 or 1.

More Abridge problems

See Abridge hiring insights
public int[][] flipInvertAndSmooth(int[][] image) {
    // Write your code here.
}
image[[0,0,0],[0,0,0],[1,0,0]]
expected[[1,1,1],[1,0,0],[1,0,0]]
Checking account…