파비의 매일매일 공부기록

2023.12.20 Today's Challenge 본문

Problem Solving/LeetCode

2023.12.20 Today's Challenge

fabichoi 2023. 12. 20. 23:45

https://leetcode.com/problems/image-smoother/

 

Image Smoother - LeetCode

Can you solve this real interview question? 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 nin

leetcode.com

class Solution:
    def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
        m, n = len(img), len(img[0])

        s_img = [[0] * n for _ in range(m)]

        for i in range(m):
            for j in range(n):
                s, cnt = 0, 0
                for x in (i-1, i, i+1):
                    for y in (j-1, j, j+1):
                        if 0 <= x < m and 0 <= y < n:
                            s += img[x][y]
                            cnt += 1
                s_img[i][j] = s // cnt

        return s_img
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.12.22 Today's Challenge  (0) 2023.12.22
2023.12.21 Today's Challenge  (0) 2023.12.21
2023.12.19 Today's Challenge  (0) 2023.12.19
2023.12.18 Today's Challenge  (0) 2023.12.18
2023.12.17 Today's Challenge  (0) 2023.12.17
Comments