Skip to content

Practice Find the Grid of Region Average

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3030
LeetCode Problem

Find the Grid of Region Average

Problem Statement

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 to threshold. 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 the rounded-down average intensities of these regions, rounded down to the nearest integer. If image[i][j] does not belong to any region, result[i][j] is equal to image[i][j]. Return the grid result.

Source: Find the Grid of Region Average on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3
Output
[[9,9,9,9],[9,9,9,9],[9,9,9,9]]

# Example 2

Input
image = [[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold = 12
Output
[[25,25,25],[27,27,27],[27,27,27],[30,30,30]]

# Example 3

Input
image = [[5,6,7],[8,9,10],[11,12,13]], threshold = 1
Output
[[5,6,7],[8,9,10],[11,12,13]]

Constraints

3 <= n, m <= 500
0 <= image[i][j] <= 255
0 <= threshold <= 255

Related LeetCode Problems

Practice more problems similar to Find the Grid of Region Average.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.