LeetCode Entry

661. Image Smoother

19.12.2023 easy 2023 kotlin

x3 average of each cell in 2D matrix

661. Image Smoother easy blog post substack image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/444

Problem TLDR

3x3 average of each cell in 2D matrix

Complexity

  • Time complexity: \(O(nm)\)

  • Space complexity: \(O(nm)\)

Code


  fun imageSmoother(img: Array<IntArray>): Array<IntArray> =
    Array(img.size) {
      val ys = (max(0, it - 1)..min(img.lastIndex, it + 1)).asSequence()
      IntArray(img[0].size) {
        val xs = (max(0, it - 1)..min(img[0].lastIndex, it + 1)).asSequence()
        ys.flatMap { y -> xs.map { img[y][it] } }.average().toInt()
      }
    }