The problem ‘Image Smoother’ can be found on LeetCode as problem number 661. Given a 2D integer matrix M
representing the grayscale of an image, you need to return the smoothed version of the image. The value of each cell in the smoothed version of the image is the average of all the 8 surrounding cells and itself. If a cell has fewer than 8 neighbors, then the average is calculated based on the available neighbors.
Here is the formal description:
Given a 2D integer matrix `M` representing the grayscale of an image, return the smoothed version of the image.
Input:
- An integer matrix `M` representing the grayscale of an image.
Output:
- Return a matrix `result` such that `result[i][j]` is the average of `M[i][j]` and all of its 8 surrounding cells. Rounded down to the nearest integer in case of fractional values.
M
.result
.Here is the C++ code to solve the problem:
#include <vector>
#include <cmath>
using namespace std;
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
if (M.empty()) return {};
int rows = M.size();
int cols = M[0].size();
vector<vector<int>> result(rows, vector<int>(cols, 0));
// Directions array representing 8 possible movements and the cell itself
constexpr int directions[9][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 0}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
int sum = 0, count = 0;
for (auto dir : directions) {
int nr = r + dir[0];
int nc = c + dir[1];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols) {
sum += M[nr][nc];
++count;
}
}
result[r][c] = sum / count; // Using integer division for rounding down
}
}
return result;
}
};
The time complexity of this solution is O(n * m), where n
is the number of rows and m
is the number of columns of the matrix M
. This is because we iterate through each cell exactly once and perform a constant amount of work for each cell in checking the neighbors.
The space complexity is O(n * m) as well, due to the storage of the resultant matrix.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?