forked from deepaktalwardt/interview-prep-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path661-image-smoother.cpp
More file actions
25 lines (25 loc) · 902 Bytes
/
661-image-smoother.cpp
File metadata and controls
25 lines (25 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int rows = M.size();
int cols = M[0].size();
vector<vector<int>> neighbors = {{-1,-1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
vector<vector<int>> result(rows, vector<int>(cols, 0));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int sum = 0;
int num = 0;
for (vector<int>& n : neighbors) {
int x = i + n[0];
int y = j + n[1];
if (x >= 0 && y >= 0 && x < rows && y < cols) {
sum += M[x][y];
num++;
}
}
result[i][j] = sum/num;
}
}
return result;
}
};