|
| 1 | +--- |
| 2 | +id: find-the-grid-of-region-average |
| 3 | +title: Find the Grid of Region Average |
| 4 | +sidebar_label: Find the Grid of Region Average |
| 5 | +tags: [Array, Matrix, Graph, BFS, DFS, Python, C++, Java] |
| 6 | +description: Calculate the 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. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +You are given an $m \times 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`. |
| 14 | + |
| 15 | +Two pixels are adjacent if they share an edge. |
| 16 | + |
| 17 | +A region is a $3 \times 3$ subgrid where the absolute difference in intensity between any two adjacent pixels is less than or equal to `threshold`. |
| 18 | + |
| 19 | +All pixels in a region belong to that region; note that a pixel can belong to multiple regions. |
| 20 | + |
| 21 | +You need to calculate an $m \times 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]`. |
| 22 | + |
| 23 | +Return the grid `result`. |
| 24 | + |
| 25 | +### Examples |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | +``` |
| 29 | +Input: image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3 |
| 30 | +Output: [[9,9,9,9],[9,9,9,9],[9,9,9,9]] |
| 31 | +``` |
| 32 | +**Explanation:** |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | + |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- $3 \leq n, m \leq 500$ |
| 40 | +- $0 \leq image[i][j] \leq 255$ |
| 41 | +- $0 \leq threshold \leq 255$ |
| 42 | + |
| 43 | +## Solution |
| 44 | + |
| 45 | +### Intuition |
| 46 | + |
| 47 | +To solve this problem, we need to identify all valid $3 \times 3$ regions, calculate their average intensities, and then determine the regions to which each pixel belongs. For each pixel, we then compute the required average intensity based on the regions it belongs to. |
| 48 | + |
| 49 | +### Time Complexity and Space Complexity Analysis |
| 50 | + |
| 51 | +- **Time Complexity**: $O(m \times n)$, where $m$ and $n$ are the dimensions of the grid. This complexity arises from the need to process each pixel and its regions. |
| 52 | +- **Space Complexity**: $O(m \times n)$, required to store the resulting grid and any auxiliary data structures. |
| 53 | + |
| 54 | +### Code |
| 55 | + |
| 56 | +#### Python |
| 57 | + |
| 58 | +```python |
| 59 | +def findRegionAverages(image, threshold): |
| 60 | + m, n = len(image), len(image[0]) |
| 61 | + result = [[image[i][j] for j in range(n)] for i in range(m)] |
| 62 | + |
| 63 | + def is_valid_region(i, j): |
| 64 | + region = [] |
| 65 | + for x in range(i, i+3): |
| 66 | + for y in range(j, j+3): |
| 67 | + region.append(image[x][y]) |
| 68 | + for x in range(i, i+3): |
| 69 | + for y in range(j, j+3): |
| 70 | + if x > i and abs(image[x][y] - image[x-1][y]) > threshold: |
| 71 | + return False |
| 72 | + if y > j and abs(image[x][y] - image[x][y-1]) > threshold: |
| 73 | + return False |
| 74 | + return region |
| 75 | + |
| 76 | + for i in range(m-2): |
| 77 | + for j in range(n-2): |
| 78 | + region = is_valid_region(i, j) |
| 79 | + if region: |
| 80 | + avg = sum(region) // 9 |
| 81 | + for x in range(i, i+3): |
| 82 | + for y in range(j, j+3): |
| 83 | + if result[x][y] == image[x][y]: |
| 84 | + result[x][y] = avg |
| 85 | + else: |
| 86 | + result[x][y] = (result[x][y] + avg) // 2 |
| 87 | + |
| 88 | + return result |
| 89 | +``` |
| 90 | + |
| 91 | +#### C++ |
| 92 | +```cpp |
| 93 | +#include <vector> |
| 94 | +#include <cmath> |
| 95 | + |
| 96 | +using namespace std; |
| 97 | + |
| 98 | +vector<vector<int>> findRegionAverages(vector<vector<int>>& image, int threshold) { |
| 99 | + int m = image.size(); |
| 100 | + int n = image[0].size(); |
| 101 | + vector<vector<int>> result(m, vector<int>(n, 0)); |
| 102 | + |
| 103 | + auto is_valid_region = [&](int i, int j) -> bool { |
| 104 | + vector<int> region; |
| 105 | + for (int x = i; x < i + 3; ++x) { |
| 106 | + for (int y = j; y < j + 3; ++y) { |
| 107 | + region.push_back(image[x][y]); |
| 108 | + } |
| 109 | + } |
| 110 | + for (int x = i; x < i + 3; ++x) { |
| 111 | + for (int y = j; y < j + 3; ++y) { |
| 112 | + if (x > i && abs(image[x][y] - image[x-1][y]) > threshold) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + if (y > j && abs(image[x][y] - image[x][y-1]) > threshold) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return true; |
| 121 | + }; |
| 122 | + |
| 123 | + for (int i = 0; i < m - 2; ++i) { |
| 124 | + for (int j = 0; j < n - 2; ++j) { |
| 125 | + if (is_valid_region(i, j)) { |
| 126 | + int sum = 0; |
| 127 | + for (int x = i; x < i + 3; ++x) { |
| 128 | + for (int y = j; y < j + 3; ++y) { |
| 129 | + sum += image[x][y]; |
| 130 | + } |
| 131 | + } |
| 132 | + int avg = sum / 9; |
| 133 | + for (int x = i; x < i + 3; ++x) { |
| 134 | + for (int y = j; y < j + 3; ++y) { |
| 135 | + result[x][y] = (result[x][y] == 0) ? avg : (result[x][y] + avg) / 2; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + for (int i = 0; i < m; ++i) { |
| 143 | + for (int j = 0; j < n; ++j) { |
| 144 | + if (result[i][j] == 0) { |
| 145 | + result[i][j] = image[i][j]; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return result; |
| 151 | +} |
| 152 | +``` |
0 commit comments