You are given m x n grid image which represents a grayscale image, where
image[i][j] represents a pixel with intensity in the range [0..255]. You are also given a non-negative integer threshold.
Two pixels are adjacent if they share an edge.
A region is a 3 x 3 subgrid where the absolute difference in intensity between any two adjacent pixels is less than or equal tothreshold.
All pixels in a region belong to that region, note that a pixel can belong to
multiple regions.
You need to calculate a m x n grid result, where result[i][j] is the
average intensity of the regions to which image[i][j] belongs, rounded down to the nearest integer. If image[i][j] belongs to multiple regions,
result[i][j] is the average of therounded-down average intensities of these regions, rounded down to the nearest integer. If image[i][j]
doesnot belong to any region, result[i][j] is equal toimage[i][j].
Input: image =[[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold =3Output: [[9,9,9,9],[9,9,9,9],[9,9,9,9]]Explanation:

There are two regions as illustrated above. The average intensity of the first
region is9,while the average intensity of the second region is9.67 which isrounded down to 9. The average intensity of both of the regions is(9+9)/2=9. As all the pixels belong to either region 1, region 2, or both of them,the intensity of every pixel in the result is9.Please note that the rounded-down values are used when calculating the average
of multiple regions, hence the calculation is done using 9 as the average
intensity of region 2, not 9.67.
Input: image =[[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold =12Output: [[25,25,25],[27,27,27],[27,27,27],[30,30,30]]Explanation:

There are two regions as illustrated above. The average intensity of the first
region is25,while the average intensity of the second region is30. The
average intensity of both of the regions is(25+30)/2=27.5 which isrounded down to 27.All the pixels in row 0 of the image belong to region 1, hence all the pixels
in row 0in the result are 25. Similarly, all the pixels in row 3in the
result are 30. The pixels in rows 1 and 2 of the image belong to region 1 and
region 2, hence their assigned value is27in the result.
Input: image =[[5,6,7],[8,9,10],[11,12,13]], threshold =1Output: [[5,6,7],[8,9,10],[11,12,13]]Explanation:
There is only one `3 x 3` subgrid,while it does not have the condition on
difference of adjacent pixels,for example, the difference between
`image[0][0]` and `image[1][0]`is`|5 - 8| = 3 > threshold = 1`. None of them
belong to any valid regions, so the `result` should be the same as `image`.
For each possible 3x3 region, check if all adjacent pixels have an absolute difference ≤ threshold. If valid, compute the region’s average and assign it to all pixels in the region. For each pixel, average the values of all regions it belongs to. If a pixel belongs to no region, its value remains unchanged.
For each possible 3x3 region (top-left at (i, j)), check all adjacent pairs for the threshold condition.
If valid, compute the region’s average (sum of 9 pixels // 9).
For each pixel in the region, keep a list of region averages it belongs to.
For each pixel, if it belongs to at least one region, set result[i][j] to the average of its region averages (rounded down). Otherwise, set result[i][j] = image[i][j].
classSolution {
public: vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {
int m = image.size(), n = image[0].size();
vector<vector<vector<int>>> regions(m, vector<vector<int>>(n));
vector<pair<int, int>> dirs = {{0,1},{1,0}};
for (int i =0; i +2< m; ++i) {
for (int j =0; j +2< n; ++j) {
bool valid = true;
for (int x =0; x <3&& valid; ++x) {
for (int y =0; y <3&& valid; ++y) {
for (auto& d : dirs) {
int ni = i + x + d.first, nj = j + y + d.second;
if (ni < i || ni > i+2|| nj < j || nj > j+2) continue;
if (abs(image[i+x][j+y] - image[ni][nj]) > threshold) {
valid = false;
break;
}
}
}
}
if (valid) {
int sum =0;
for (int x =0; x <3; ++x)
for (int y =0; y <3; ++y)
sum += image[i+x][j+y];
int avg = sum /9;
for (int x =0; x <3; ++x)
for (int y =0; y <3; ++y)
regions[i+x][j+y].push_back(avg);
}
}
}
vector<vector<int>> res(m, vector<int>(n));
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
if (!regions[i][j].empty()) {
int s =0;
for (int v : regions[i][j]) s += v;
res[i][j] = s / regions[i][j].size();
} else {
res[i][j] = image[i][j];
}
}
}
return res;
}
};
classSolution {
publicint[][]resultGrid(int[][] image, int threshold) {
int m = image.length, n = image[0].length;
List<Integer>[][] regions =new ArrayList[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
regions[i][j]=new ArrayList<>();
int[][] dirs = {{0,1},{1,0}};
for (int i = 0; i + 2 < m; ++i) {
for (int j = 0; j + 2 < n; ++j) {
boolean valid =true;
for (int x = 0; x < 3 && valid; ++x)
for (int y = 0; y < 3 && valid; ++y)
for (int[] d : dirs) {
int ni = i + x + d[0], nj = j + y + d[1];
if (ni < i || ni > i+2 || nj < j || nj > j+2) continue;
if (Math.abs(image[i+x][j+y]- image[ni][nj]) > threshold) {
valid =false;
break;
}
}
if (valid) {
int sum = 0;
for (int x = 0; x < 3; ++x)
for (int y = 0; y < 3; ++y)
sum += image[i+x][j+y];
int avg = sum / 9;
for (int x = 0; x < 3; ++x)
for (int y = 0; y < 3; ++y)
regions[i+x][j+y].add(avg);
}
}
}
int[][] res =newint[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (!regions[i][j].isEmpty()) {
int s = 0;
for (int v : regions[i][j]) s += v;
res[i][j]= s / regions[i][j].size();
} else {
res[i][j]= image[i][j];
}
return res;
}
}
classSolution {
funresultGrid(image: Array<IntArray>, threshold: Int): Array<IntArray> {
val m = image.size
val n = image[0].size
val regions = Array(m) { Array(n) { mutableListOf<Int>() } }
val dirs = arrayOf(intArrayOf(0,1), intArrayOf(1,0))
for (i in0..m-3) {
for (j in0..n-3) {
var valid = truefor (x in0..2) for (y in0..2) for (d in dirs) {
val ni = i + x + d[0]
val nj = j + y + d[1]
if (ni < i || ni > i+2|| nj < j || nj > j+2) continueif (kotlin.math.abs(image[i+x][j+y] - image[ni][nj]) > threshold) {
valid = falsebreak }
}
if (valid) {
var sum = 0for (x in0..2) for (y in0..2) sum += image[i+x][j+y]
val avg = sum / 9for (x in0..2) for (y in0..2) regions[i+x][j+y].add(avg)
}
}
}
val res = Array(m) { IntArray(n) }
for (i in0 until m) for (j in0 until n)
res[i][j] = if (regions[i][j].isNotEmpty()) regions[i][j].sum() / regions[i][j].size else image[i][j]
return res
}
}
classSolution:
defresultGrid(self, image: list[list[int]], threshold: int) -> list[list[int]]:
m, n = len(image), len(image[0])
regions = [[[] for _ in range(n)] for _ in range(m)]
dirs = [(0,1),(1,0)]
for i in range(m-2):
for j in range(n-2):
valid =Truefor x in range(3):
for y in range(3):
for dx, dy in dirs:
ni, nj = i+x+dx, j+y+dy
if ni < i or ni > i+2or nj < j or nj > j+2:
continueif abs(image[i+x][j+y] - image[ni][nj]) > threshold:
valid =Falsebreakifnot valid:
breakifnot valid:
breakif valid:
s = sum(image[i+x][j+y] for x in range(3) for y in range(3))
avg = s //9for x in range(3):
for y in range(3):
regions[i+x][j+y].append(avg)
res = [[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
if regions[i][j]:
res[i][j] = sum(regions[i][j]) // len(regions[i][j])
else:
res[i][j] = image[i][j]
return res
impl Solution {
pubfnresult_grid(image: Vec<Vec<i32>>, threshold: i32) -> Vec<Vec<i32>> {
let m = image.len();
let n = image[0].len();
letmut regions =vec![vec![vec![]; n]; m];
let dirs = [(0,1),(1,0)];
for i in0..m-2 {
for j in0..n-2 {
letmut valid =true;
'outer: for x in0..3 {
for y in0..3 {
for&(dx,dy) in&dirs {
let ni = i+x+dx;
let nj = j+y+dy;
if ni < i || ni > i+2|| nj < j || nj > j+2 { continue; }
if (image[i+x][j+y] - image[ni][nj]).abs() > threshold {
valid =false;
break 'outer;
}
}
}
}
if valid {
letmut sum =0;
for x in0..3 {
for y in0..3 {
sum += image[i+x][j+y];
}
}
let avg = sum /9;
for x in0..3 {
for y in0..3 {
regions[i+x][j+y].push(avg);
}
}
}
}
}
letmut res =vec![vec![0; n]; m];
for i in0..m {
for j in0..n {
if!regions[i][j].is_empty() {
let s: i32= regions[i][j].iter().sum();
res[i][j] = s / regions[i][j].len() asi32;
} else {
res[i][j] = image[i][j];
}
}
}
res
}
}