#3030
Find the Grid of Region Average
MediumArrayMatrixUnion-FindGraph Traversal
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(m * n * 9) = O(m * n) | O(m * n * α(m * n)) |
| Space | O(1) | O(m * n) |
💡
Intuition
Time O(m * n * α(m * n))Space O(m * n)
The optimal approach uses a more efficient method to identify regions by leveraging a union-find structure or a similar grouping technique. This allows us to efficiently calculate the average intensity of overlapping regions without redundant checks.
⚙️
Algorithm
3 steps- 1Step 1: Create a union-find structure to group pixels into regions based on the threshold condition.
- 2Step 2: For each pixel, check its neighbors and union them if they meet the threshold condition.
- 3Step 3: After forming regions, calculate the average intensity for each region and update the result grid accordingly.
solution.py59 lines
1# Full working Python code
2class UnionFind:
3 def __init__(self, size):
4 self.parent = list(range(size))
5 self.rank = [1] * size
6 self.sum = [0] * size
7 self.count = [0] * size
8
9 def find(self, x):
10 if self.parent[x] != x:
11 self.parent[x] = self.find(self.parent[x])
12 return self.parent[x]
13
14 def union(self, x, y, image):
15 rootX = self.find(x)
16 rootY = self.find(y)
17 if rootX != rootY:
18 if self.rank[rootX] > self.rank[rootY]:
19 self.parent[rootY] = rootX
20 self.sum[rootX] += self.sum[rootY]
21 self.count[rootX] += self.count[rootY]
22 elif self.rank[rootX] < self.rank[rootY]:
23 self.parent[rootX] = rootY
24 self.sum[rootY] += self.sum[rootX]
25 self.count[rootY] += self.count[rootX]
26 else:
27 self.parent[rootY] = rootX
28 self.sum[rootX] += self.sum[rootY]
29 self.count[rootX] += self.count[rootY]
30 self.rank[rootX] += 1
31
32 def add_pixel(self, index, value):
33 self.sum[index] = value
34 self.count[index] = 1
35
36
37def find_region_average(image, threshold):
38 m, n = len(image), len(image[0])
39 uf = UnionFind(m * n)
40 for i in range(m):
41 for j in range(n):
42 index = i * n + j
43 uf.add_pixel(index, image[i][j])
44 for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
45 ni, nj = i + dx, j + dy
46 if 0 <= ni < m and 0 <= nj < n:
47 if abs(image[i][j] - image[ni][nj]) <= threshold:
48 uf.union(index, ni * n + nj, image)
49 result = [[0] * n for _ in range(m)]
50 for i in range(m):
51 for j in range(n):
52 index = i * n + j
53 root = uf.find(index)
54 if uf.count[root] > 0:
55 result[i][j] = uf.sum[root] // uf.count[root]
56 else:
57 result[i][j] = image[i][j]
58 return result
59ℹ
Complexity note: The time complexity is O(m * n * α(m * n)) due to the union-find operations, where α is the inverse Ackermann function, which grows very slowly. The space complexity is O(m * n) for storing the union-find structure.
- 1Regions can overlap, and a pixel can belong to multiple regions.
- 2Efficiently grouping pixels using union-find can significantly reduce redundant calculations.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.