|
62 | 62 | <!-- tabs:start -->
|
63 | 63 |
|
64 | 64 | ```python
|
| 65 | +class Solution: |
| 66 | + def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]: |
| 67 | + n, m = len(image), len(image[0]) |
| 68 | + ans = [[0] * m for _ in range(n)] |
| 69 | + ct = [[0] * m for _ in range(n)] |
| 70 | + |
| 71 | + for i in range(n - 2): |
| 72 | + for j in range(m - 2): |
| 73 | + region = True |
| 74 | + for k in range(3): |
| 75 | + for l in range(2): |
| 76 | + region &= abs(image[i + k][j + l] - image[i + k][j + l + 1]) <= threshold |
| 77 | + for k in range(2): |
| 78 | + for l in range(3): |
| 79 | + region &= abs(image[i + k][j + l] - image[i + k + 1][j + l]) <= threshold |
| 80 | + |
| 81 | + if region: |
| 82 | + tot = 0 |
| 83 | + for k in range(3): |
| 84 | + for l in range(3): |
| 85 | + tot += image[i + k][j + l] |
| 86 | + for k in range(3): |
| 87 | + for l in range(3): |
| 88 | + ct[i + k][j + l] += 1 |
| 89 | + ans[i + k][j + l] += tot // 9 |
| 90 | + |
| 91 | + for i in range(n): |
| 92 | + for j in range(m): |
| 93 | + if ct[i][j] == 0: |
| 94 | + ans[i][j] = image[i][j] |
| 95 | + else: |
| 96 | + ans[i][j] //= ct[i][j] |
| 97 | + |
| 98 | + return ans |
65 | 99 |
|
66 | 100 | ```
|
67 | 101 |
|
68 | 102 | ```java
|
| 103 | +class Solution { |
| 104 | + public int[][] resultGrid(int[][] image, int threshold) { |
| 105 | + int n = image.length; |
| 106 | + int m = image[0].length; |
| 107 | + int[][] ans = new int[n][m]; |
| 108 | + int[][] ct = new int[n][m]; |
| 109 | + for (int i = 0; i + 2 < n; ++i) { |
| 110 | + for (int j = 0; j + 2 < m; ++j) { |
| 111 | + boolean region = true; |
| 112 | + for (int k = 0; k < 3; ++k) { |
| 113 | + for (int l = 0; l < 2; ++l) { |
| 114 | + region &= Math.abs(image[i + k][j + l] - image[i + k][j + l + 1]) <= threshold; |
| 115 | + } |
| 116 | + } |
| 117 | + for (int k = 0; k < 2; ++k) { |
| 118 | + for (int l = 0; l < 3; ++l) { |
| 119 | + region &= Math.abs(image[i + k][j + l] - image[i + k + 1][j + l]) <= threshold; |
| 120 | + } |
| 121 | + } |
| 122 | + if (region) { |
| 123 | + int tot = 0; |
| 124 | + for (int k = 0; k < 3; ++k) { |
| 125 | + for (int l = 0; l < 3; ++l) { |
| 126 | + tot += image[i + k][j + l]; |
| 127 | + } |
| 128 | + } |
| 129 | + for (int k = 0; k < 3; ++k) { |
| 130 | + for (int l = 0; l < 3; ++l) { |
| 131 | + ct[i + k][j + l]++; |
| 132 | + ans[i + k][j + l] += tot / 9; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + for (int i = 0; i < n; ++i) { |
| 139 | + for (int j = 0; j < m; ++j) { |
| 140 | + if (ct[i][j] == 0) { |
| 141 | + ans[i][j] = image[i][j]; |
| 142 | + } else { |
| 143 | + ans[i][j] /= ct[i][j]; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return ans; |
| 148 | + } |
| 149 | +} |
69 | 150 |
|
70 | 151 | ```
|
71 | 152 |
|
72 | 153 | ```cpp
|
73 |
| - |
| 154 | +class Solution { |
| 155 | +public: |
| 156 | + vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) { |
| 157 | + int n = image.size(), m = image[0].size(); |
| 158 | + vector<vector<int>> ans(n, vector<int>(m)); |
| 159 | + vector<vector<int>> ct(n, vector<int>(m)); |
| 160 | + for (int i = 0; i + 2 < n; ++i) { |
| 161 | + for (int j = 0; j + 2 < m; ++j) { |
| 162 | + bool region = true; |
| 163 | + for (int k = 0; k < 3; ++k) { |
| 164 | + for (int l = 0; l < 2; ++l) { |
| 165 | + region &= abs(image[i + k][j + l] - image[i + k][j + l + 1]) <= threshold; |
| 166 | + } |
| 167 | + } |
| 168 | + for (int k = 0; k < 2; ++k) { |
| 169 | + for (int l = 0; l < 3; ++l) { |
| 170 | + region &= abs(image[i + k][j + l] - image[i + k + 1][j + l]) <= threshold; |
| 171 | + } |
| 172 | + } |
| 173 | + if (region) { |
| 174 | + int tot = 0; |
| 175 | + for (int k = 0; k < 3; ++k) { |
| 176 | + for (int l = 0; l < 3; ++l) { |
| 177 | + tot += image[i + k][j + l]; |
| 178 | + } |
| 179 | + } |
| 180 | + for (int k = 0; k < 3; ++k) { |
| 181 | + for (int l = 0; l < 3; ++l) { |
| 182 | + ct[i + k][j + l]++; |
| 183 | + ans[i + k][j + l] += tot / 9; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + for (int i = 0; i < n; ++i) { |
| 190 | + for (int j = 0; j < m; ++j) { |
| 191 | + if (ct[i][j] == 0) { |
| 192 | + ans[i][j] = image[i][j]; |
| 193 | + } else { |
| 194 | + ans[i][j] /= ct[i][j]; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + return ans; |
| 199 | + } |
| 200 | +}; |
74 | 201 | ```
|
75 | 202 |
|
76 | 203 | ```go
|
| 204 | +func resultGrid(image [][]int, threshold int) [][]int { |
| 205 | + n := len(image) |
| 206 | + m := len(image[0]) |
| 207 | + ans := make([][]int, n) |
| 208 | + ct := make([][]int, n) |
| 209 | + for i := range ans { |
| 210 | + ans[i] = make([]int, m) |
| 211 | + ct[i] = make([]int, m) |
| 212 | + } |
| 213 | + for i := 0; i+2 < n; i++ { |
| 214 | + for j := 0; j+2 < m; j++ { |
| 215 | + region := true |
| 216 | + for k := 0; k < 3; k++ { |
| 217 | + for l := 0; l < 2; l++ { |
| 218 | + region = region && abs(image[i+k][j+l]-image[i+k][j+l+1]) <= threshold |
| 219 | + } |
| 220 | + } |
| 221 | + for k := 0; k < 2; k++ { |
| 222 | + for l := 0; l < 3; l++ { |
| 223 | + region = region && abs(image[i+k][j+l]-image[i+k+1][j+l]) <= threshold |
| 224 | + } |
| 225 | + } |
| 226 | + if region { |
| 227 | + tot := 0 |
| 228 | + for k := 0; k < 3; k++ { |
| 229 | + for l := 0; l < 3; l++ { |
| 230 | + tot += image[i+k][j+l] |
| 231 | + } |
| 232 | + } |
| 233 | + for k := 0; k < 3; k++ { |
| 234 | + for l := 0; l < 3; l++ { |
| 235 | + ct[i+k][j+l]++ |
| 236 | + ans[i+k][j+l] += tot / 9 |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + for i := 0; i < n; i++ { |
| 243 | + for j := 0; j < m; j++ { |
| 244 | + if ct[i][j] == 0 { |
| 245 | + ans[i][j] = image[i][j] |
| 246 | + } else { |
| 247 | + ans[i][j] /= ct[i][j] |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + return ans |
| 252 | +} |
| 253 | +func abs(x int) int { |
| 254 | + if x < 0 { |
| 255 | + return -x |
| 256 | + } |
| 257 | + return x |
| 258 | +} |
| 259 | +``` |
| 260 | + |
| 261 | +```ts |
| 262 | +function resultGrid(image: number[][], threshold: number): number[][] { |
| 263 | + const n: number = image.length; |
| 264 | + const m: number = image[0].length; |
| 265 | + const ans: number[][] = new Array(n).fill(0).map(() => new Array(m).fill(0)); |
| 266 | + const ct: number[][] = new Array(n).fill(0).map(() => new Array(m).fill(0)); |
| 267 | + for (let i = 0; i + 2 < n; ++i) { |
| 268 | + for (let j = 0; j + 2 < m; ++j) { |
| 269 | + let region: boolean = true; |
| 270 | + for (let k = 0; k < 3; ++k) { |
| 271 | + for (let l = 0; l < 2; ++l) { |
| 272 | + region &&= Math.abs(image[i + k][j + l] - image[i + k][j + l + 1]) <= threshold; |
| 273 | + } |
| 274 | + } |
| 275 | + for (let k = 0; k < 2; ++k) { |
| 276 | + for (let l = 0; l < 3; ++l) { |
| 277 | + region &&= Math.abs(image[i + k][j + l] - image[i + k + 1][j + l]) <= threshold; |
| 278 | + } |
| 279 | + } |
| 280 | + if (region) { |
| 281 | + let tot: number = 0; |
77 | 282 |
|
| 283 | + for (let k = 0; k < 3; ++k) { |
| 284 | + for (let l = 0; l < 3; ++l) { |
| 285 | + tot += image[i + k][j + l]; |
| 286 | + } |
| 287 | + } |
| 288 | + for (let k = 0; k < 3; ++k) { |
| 289 | + for (let l = 0; l < 3; ++l) { |
| 290 | + ct[i + k][j + l]++; |
| 291 | + ans[i + k][j + l] += Math.floor(tot / 9); |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + for (let i = 0; i < n; ++i) { |
| 298 | + for (let j = 0; j < m; ++j) { |
| 299 | + if (ct[i][j] === 0) { |
| 300 | + ans[i][j] = image[i][j]; |
| 301 | + } else { |
| 302 | + ans[i][j] = Math.floor(ans[i][j] / ct[i][j]); |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + return ans; |
| 307 | +} |
78 | 308 | ```
|
79 | 309 |
|
80 | 310 | <!-- tabs:end -->
|
|
0 commit comments