LeetCode 661: Image Smoother Solution

Master LeetCode problem 661 (Image Smoother), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

661. Image Smoother

Problem Explanation

Explanation

To solve this problem, we will iterate over each cell in the matrix and calculate the average value of the cell and its surrounding cells within the 3x3 grid. We will then update the corresponding cell in a new matrix with the floor value of this average. If a cell does not have all 8 surrounding cells, we will only consider the available cells in the average calculation.

  • Algorithm:

    1. Create a new matrix of the same size as the input matrix to store the smoothed image.
    2. Iterate over each cell in the input matrix.
    3. For each cell, calculate the sum of the cell and its surrounding cells.
    4. Calculate the average value by dividing the sum by the total number of cells considered.
    5. Update the corresponding cell in the new matrix with the floor value of the average.
    6. Return the new matrix as the smoothed image.
  • Time Complexity: O(m*n) where m is the number of rows and n is the number of columns in the matrix.

  • Space Complexity: O(m*n) for the new matrix to store the smoothed image.

Solution Code

class Solution {
    public int[][] imageSmoother(int[][] img) {
        int m = img.length;
        int n = img[0].length;
        int[][] result = new int[m][n];
        
        int[][] dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int sum = 0;
                int count = 0;
                for (int[] dir : dirs) {
                    int ni = i + dir[0];
                    int nj = j + dir[1];
                    if (ni >= 0 && ni < m && nj >= 0 && nj < n) {
                        sum += img[ni][nj];
                        count++;
                    }
                }
                result[i][j] = (int) Math.floor((double) sum / count);
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 661 (Image Smoother)?

This page provides optimized solutions for LeetCode problem 661 (Image Smoother) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 661 (Image Smoother)?

The time complexity for LeetCode 661 (Image Smoother) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 661 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 661 in Java, C++, or Python.

Back to LeetCode Solutions