661. Image Smoother LeetCode Solution
In this guide, you will get 661. Image Smoother LeetCode Solution with the best time and space complexity. The solution to Image Smoother problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Image Smoother solution in C++
- Image Smoother solution in Java
- Image Smoother solution in Python
- Additional Resources
Problem Statement of Image Smoother
An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.
Example 1:
Input: img = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[0,0,0],[0,0,0],[0,0,0]]
Explanation:
For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
Example 2:
Input: img = [[100,200,100],[200,50,200],[100,200,100]]
Output: [[137,141,137],[141,138,141],[137,141,137]]
Explanation:
For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
Constraints:
m == img.length
n == img[i].length
1 <= m, n <= 200
0 <= img[i][j] <= 255
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
661. Image Smoother LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
const int m = M.size();
const int n = M[0].size();
vector<vector<int>> ans(m, vector<int>(n));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
int ones = 0;
int count = 0;
for (int x = max(0, i - 1); x < min(m, i + 2); ++x)
for (int y = max(0, j - 1); y < min(n, j + 2); ++y) {
ones += M[x][y];
++count;
}
ans[i][j] = ones / count;
}
return ans;
}
};
/* code provided by PROGIEZ */
661. Image Smoother LeetCode Solution in Java
class Solution {
public int[][] imageSmoother(int[][] M) {
final int m = M.length;
final int n = M[0].length;
int ans[][] = new int[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
int ones = 0;
int count = 0;
for (int y = Math.max(0, i - 1); y < Math.min(m, i + 2); ++y)
for (int x = Math.max(0, j - 1); x < Math.min(n, j + 2); ++x) {
ones += M[y][x];
++count;
}
ans[i][j] = ones / count;
}
return ans;
}
}
// code provided by PROGIEZ
661. Image Smoother LeetCode Solution in Python
class Solution:
def imageSmoother(self, M: list[list[int]]) -> list[list[int]]:
m = len(M)
n = len(M[0])
ans = [[0 for j in range(n)] for i in range(m)]
for i in range(m):
for j in range(n):
ones = 0
count = 0
for y in range(max(0, i - 1), min(m, i + 2)):
for x in range(max(0, j - 1), min(n, j + 2)):
ones += M[y][x]
count += 1
ans[i][j] = ones // count
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.