↓Skip to main content
  1. LeetCode/

LeetCode 661: Image Smoother

·2 mins· ·
LeetCode Easy Array Matrix
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-29

  • Submissions: Runtime Error → Wrong Answer ×3 → Runtime Error ×4 → Accepted ×2

Approach
#

For each cell enumerate dx, dy ∈ {-1, 0, 1} (Matrix Template 3), keep in-range neighbors, and take sum // count. Write into a new matrix, or later cells read already-smoothed values. Build it with a comprehension; every row of [[0] * n] * m is the same list.

My Attempts
#

This took 10 submissions:

  • The result was [[0] * n] * m, so every row was the same list and the last row overwrote all of them: [[45], [69], [2]] returned 35 in every row. The first version also used /, producing values like 41.5.
  • Switching to appending as I went, the condition for starting a new row was wrong and kept raising IndexError; i == len(res) passed.

Solution
#

class Solution:
    def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
        m, n = len(img), len(img[0])
        res = [[0] * n for _ in range(m)]
        for i in range(m):
            for j in range(n):
                total = count = 0
                for x in range(i - 1, i + 2):
                    for y in range(j - 1, j + 2):
                        if 0 <= x < m and 0 <= y < n:
                            total += img[x][y]
                            count += 1
                res[i][j] = total // count
        return res

Complexity
#

Time O(9mn), space O(mn) (output).

Mistakes to Avoid
#

  • Every row of [[0] * n] * m is the same list, so writing one writes all.
  • The average is floored; use //.

Interview Explanation
#

For each cell I enumerate its 3 × 3 neighborhood, sum the in-range cells, floor-divide by their count, and write into a new matrix. Time O(9mn).

Related

LeetCode 566: Reshape the Matrix
·2 mins
LeetCode Easy Array Matrix Simulation
LeetCode 566 note: return the original if the element counts differ; otherwise element k goes to (k // c, k % c)
LeetCode 463: Island Perimeter
·2 mins
LeetCode Easy Array Depth-First-Search Breadth-First-Search Matrix
LeetCode 463 note: each land cell contributes 4 edges, minus 2 for each land neighbor above or to the left
LeetCode 628: Maximum Product of Three Numbers
·1 min
LeetCode Easy Array Math Sorting
LeetCode 628 note: after sorting there are only two candidates: the three largest, or the two smallest (negatives) times the largest
LeetCode 645: Set Mismatch
·2 mins
LeetCode Easy Array Hash-Table Bit-Manipulation Sorting
LeetCode 645 note: use sums: missing = sum of 1..n - sum of the set, duplicate = array sum - sum of the set
LeetCode 598: Range Addition II
·2 mins
LeetCode Easy Array Math
LeetCode 598 note: every operation covers the top-left corner, so the most-incremented cells form the intersection of all operations: min(a) × min(b)
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum