Flip, Invert, and Smooth a Binary Image
Problem statement
Given a rectangular binary matrix image, perform two transformations without modifying the input.
- Flip every row horizontally, then invert each bit.
- 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 3region.
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
0or1.