#3030

Find the Grid of Region Average

expert · 1015 · lc medium +32 · 43.3% accepted · 91 likes · top 25%

Description

You are given an m x n grayscale image image where image[i][j] is a pixel intensity in [0..255], and a non-negative integer threshold.

Two pixels are adjacent if they share an edge.

A region is a 3 x 3 subgrid where every pair of adjacent pixels differs by at most threshold.

Compute a result grid where each pixel's value is:
- The floor-averaged intensity of all regions it belongs to (averaged over their rounded-down averages), if it belongs to any region.
- Its original value, if it belongs to no region.

Return the result grid.

Code

1
2
3