← Home

661. Image Smoother

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
661

This is one of those problems where the clean idea matters more than the amount of code. For 661. Image Smoother, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The solution is organized around the main LeetCode entry point and a few local helpers.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 148 ms, faster than 82.86% of C++ online submissions for Image Smoother.
02//Memory Usage: 17.5 MB, less than 100.00% of C++ online submissions for Image Smoother.
03
04class Solution {
05public:
06    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
07        int m = M.size(), n = M[0].size();
08        
09        vector<vector<int>> ans(m, vector<int>(n));
10        vector<pair<int,int>> dirs = {{-1,-1}, {-1,0},{-1,1},
11                                      {0,-1},{0,0},{0,1},
12                                      {1,-1},{1,0},{1,1}};
13        
14        vector<int> v;
15        int sum = 0, count = 0;
16        for(int i = 0; i < m; i++){
17            for(int j = 0; j < n; j++){
18                sum = 0; count = 0;
19                
20                for(auto dir : dirs){
21                    int newi = i+dir.first, newj = j+dir.second;
22                    if(newi >= 0 && newi < m
23                      && newj >= 0 && newj < n){
24                        sum += M[newi][newj];
25                        count++;
26                    }
27                }
28                
29                ans[i][j] = sum/count;
30            }
31        }
32        
33        return ans;
34    }
35};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.