3030. Find the Grid of Region Average Medium
1/**
2 * [3030] Find the Grid of Region Average
3 *
4 * 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.
5 * Two pixels are adjacent if they share an edge.
6 * 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.
7 * All pixels in a region belong to that region, note that a pixel can belong to multiple regions.
8 * 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].
9 * Return the grid result.
10 *
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3</span>
14 * Output: <span class="example-io">[[9,9,9,9],[9,9,9,9],[9,9,9,9]]</span>
15 * Explanation:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example0corrected.png" style="width: 832px; height: 275px;" />
17 * There are two regions as illustrated above. The average intensity of the first region is 9, while the average intensity of the second region is 9.67 which is rounded 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 is 9.
18 * 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.
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">image = [[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold = 12</span>
23 * Output: <span class="example-io">[[25,25,25],[27,27,27],[27,27,27],[30,30,30]]</span>
24 * Explanation:
25 * <img src="https://assets.leetcode.com/uploads/2023/12/21/example1corrected.png" />
26 * There are two regions as illustrated above. The average intensity of the first region is 25, while the average intensity of the second region is 30. The average intensity of both of the regions is (25 + 30) / 2 = 27.5 which is rounded down to 27.
27 * All the pixels in row 0 of the image belong to region 1, hence all the pixels in row 0 in the result are 25. Similarly, all the pixels in row 3 in 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 is 27 in the result.
28 * </div>
29 * <strong class="example">Example 3:
30 * <div class="example-block">
31 * Input: <span class="example-io">image = [[5,6,7],[8,9,10],[11,12,13]], threshold = 1</span>
32 * Output: <span class="example-io">[[5,6,7],[8,9,10],[11,12,13]]</span>
33 * Explanation:
34 * 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.
35 * </div>
36 *
37 * Constraints:
38 *
39 * 3 <= n, m <= 500
40 * 0 <= image[i][j] <= 255
41 * 0 <= threshold <= 255
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/find-the-grid-of-region-average/
47// discuss: https://leetcode.com/problems/find-the-grid-of-region-average/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn result_grid(image: Vec<Vec<i32>>, threshold: i32) -> Vec<Vec<i32>> {
53 vec![]
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3030() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.