Skip to content

Commit

Permalink
Create 661. Image Smoother
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Dec 19, 2023
1 parent bdf787b commit ac72836
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 661. Image Smoother
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// code
int m = img.size(), n = img[0].size();
vector<vector<int>> result(m, vector<int>(n));

for (int r = 0; r < m; r++)
{
for (int c = 0; c < n; c++)
{
int sum = 0, count = 0;

// Iterate over the 3x3 neighborhood
for (int dr = -1; dr <= 1; dr++)
{
for (int dc = -1; dc <= 1; dc++)
{
int nr = r + dr;
int nc = c + dc;

// Check if the neighbor is within bounds
if ((nr >= 0) && (nr < m) && (nc >= 0) && (nc < n))
{
sum += img[nr][nc];
count++;
}
}
}

// Calculate the average and update the result matrix
result[r][c] = (sum / count);
}
}

return result;
}
};

0 comments on commit ac72836

Please sign in to comment.