Image Smoother - Complete Solution Guide
Image Smoother is LeetCode problem 661, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 sm
Detailed Explanation
The "Image Smoother" problem asks us to apply a smoothing filter to an image represented as a 2D integer matrix (grayscale). For each cell in the image, we need to calculate the average of the cell and its eight surrounding neighbors. The crucial point is that if a neighbor is outside the bounds of the image, it's not included in the average. The result should be an integer matrix where each cell contains the floor of the calculated average for the corresponding cell in the original image. The input is an m x n matrix of integers, and the output is a new m x n matrix with the smoothed values.
Solution Approach
The provided solutions iterate through each cell of the input image. For each cell, they iterate through its neighbors (including itself) within a 3x3 window. They sum the values of the valid neighbors and count the number of valid neighbors. Finally, they divide the sum by the count (integer division, which implicitly applies the 'floor' operation) to get the smoothed value for that cell, and store the smoothed value in a new output matrix.
Step-by-Step Algorithm
- Step 1: Get the dimensions (m and n) of the input image matrix.
- Step 2: Create a new matrix, `smoothed`, of the same dimensions as the input matrix to store the smoothed image.
- Step 3: Iterate through each cell (i, j) of the input matrix using nested loops.
- Step 4: For each cell (i, j), initialize `sum` and `count` to 0.
- Step 5: Iterate through the neighbors of the current cell using nested loops. The neighbor coordinates range from (i-1, j-1) to (i+1, j+1).
- Step 6: Within the inner loops, check if the neighbor coordinates (x, y) are within the bounds of the image matrix. The range should be 0 <= x < m and 0 <= y < n.
- Step 7: If the neighbor coordinates are valid, add the value of `img[x][y]` to `sum` and increment `count`.
- Step 8: After iterating through all the neighbors, calculate the smoothed value by dividing `sum` by `count` (integer division).
- Step 9: Store the smoothed value in `smoothed[i][j]`.
- Step 10: After iterating through all cells in the input matrix, return the `smoothed` matrix.
Key Insights
- Insight 1: The core logic involves iterating through each cell of the image and calculating a local average based on its neighbors. Boundary checks are vital to avoid accessing out-of-bounds elements.
- Insight 2: The use of nested loops is essential to visit each cell in the 2D matrix and then another set of (potentially bounded) nested loops to iterate through its neighbors.
- Insight 3: The min/max functions (or equivalent conditional statements in C) provide a concise way to handle edge cases where neighbor coordinates might go out of bounds, simplifying boundary checks.
- Insight 4: Using integer division ensures the floor of the average is calculated as required by the problem statement.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(m*n)
Topics
This problem involves: Array, Matrix.
Companies
Asked at: Roblox, Toptal, Verkada, Visa.