Skip to content

Commit b916175

Browse files
committed
Add solution #3030
1 parent e64a415 commit b916175

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,7 @@
21392139
3027|[Find the Number of Ways to Place People II](./solutions/3027-find-the-number-of-ways-to-place-people-ii.js)|Hard|
21402140
3028|[Ant on the Boundary](./solutions/3028-ant-on-the-boundary.js)|Easy|
21412141
3029|[Minimum Time to Revert Word to Initial State I](./solutions/3029-minimum-time-to-revert-word-to-initial-state-i.js)|Medium|
2142+
3030|[Find the Grid of Region Average](./solutions/3030-find-the-grid-of-region-average.js)|Medium|
21422143
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21432144
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21442145
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)