|
| 1 | +/** |
| 2 | + * 3030. Find the Grid of Region Average |
| 3 | + * https://leetcode.com/problems/find-the-grid-of-region-average/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are given m x n grid image which represents a grayscale image, where image[i][j] |
| 7 | + * represents a pixel with intensity in the range [0..255]. You are also given a non-negative |
| 8 | + * integer threshold. |
| 9 | + * |
| 10 | + * Two pixels are adjacent if they share an edge. |
| 11 | + * |
| 12 | + * A region is a 3 x 3 subgrid where the absolute difference in intensity between any two |
| 13 | + * adjacent pixels is less than or equal to threshold. |
| 14 | + * |
| 15 | + * All pixels in a region belong to that region, note that a pixel can belong to multiple regions. |
| 16 | + * |
| 17 | + * You need to calculate a m x n grid result, where result[i][j] is the average intensity of the |
| 18 | + * regions to which image[i][j] belongs, rounded down to the nearest integer. If image[i][j] |
| 19 | + * belongs to multiple regions, result[i][j] is the average of the rounded-down average intensities |
| 20 | + * of these regions, rounded down to the nearest integer. If image[i][j] does not belong to any |
| 21 | + * region, result[i][j] is equal to image[i][j]. |
| 22 | + * |
| 23 | + * Return the grid result. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number[][]} image |
| 28 | + * @param {number} threshold |
| 29 | + * @return {number[][]} |
| 30 | + */ |
| 31 | +var resultGrid = function(image, threshold) { |
| 32 | + const rows = image.length; |
| 33 | + const cols = image[0].length; |
| 34 | + const result = Array.from({ length: rows }, () => new Array(cols).fill(0)); |
| 35 | + const regionCount = Array.from({ length: rows }, () => new Array(cols).fill(0)); |
| 36 | + |
| 37 | + for (let i = 0; i <= rows - 3; i++) { |
| 38 | + for (let j = 0; j <= cols - 3; j++) { |
| 39 | + if (isValidRegion(i, j)) { |
| 40 | + const regionAvg = calculateRegionAverage(i, j); |
| 41 | + |
| 42 | + for (let r = i; r < i + 3; r++) { |
| 43 | + for (let c = j; c < j + 3; c++) { |
| 44 | + result[r][c] += regionAvg; |
| 45 | + regionCount[r][c]++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + for (let i = 0; i < rows; i++) { |
| 53 | + for (let j = 0; j < cols; j++) { |
| 54 | + if (regionCount[i][j] === 0) { |
| 55 | + result[i][j] = image[i][j]; |
| 56 | + } else { |
| 57 | + result[i][j] = Math.floor(result[i][j] / regionCount[i][j]); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return result; |
| 63 | + |
| 64 | + function calculateRegionAverage(startRow, startCol) { |
| 65 | + let sum = 0; |
| 66 | + for (let i = startRow; i < startRow + 3; i++) { |
| 67 | + for (let j = startCol; j < startCol + 3; j++) { |
| 68 | + sum += image[i][j]; |
| 69 | + } |
| 70 | + } |
| 71 | + return Math.floor(sum / 9); |
| 72 | + } |
| 73 | + |
| 74 | + function isValidRegion(startRow, startCol) { |
| 75 | + const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; |
| 76 | + |
| 77 | + for (let i = startRow; i < startRow + 3; i++) { |
| 78 | + for (let j = startCol; j < startCol + 3; j++) { |
| 79 | + for (const [dr, dc] of directions) { |
| 80 | + const newRow = i + dr; |
| 81 | + const newCol = j + dc; |
| 82 | + |
| 83 | + if (newRow >= startRow && newRow < startRow + 3 |
| 84 | + && newCol >= startCol && newCol < startCol + 3) { |
| 85 | + if (Math.abs(image[i][j] - image[newRow][newCol]) > threshold) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return true; |
| 93 | + } |
| 94 | +}; |
0 commit comments