LeetCode Entry

118. Pascal's Triangle

8.09.2023 easy 2023 kotlin

Pascal Triangle

118. Pascal’s Triangle easy blog post substack image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/333

Problem TLDR

Pascal Triangle

Intuition

Each row is a previous row sliding window sums concatenated with 1

Approach

Let’s write it using Kotlin API

Complexity

  • Time complexity: \(O(n^2)\)

  • Space complexity: \(O(n^2)\)

Code


    fun generate(numRows: Int) = (2..numRows)
      .runningFold(listOf(1)) { r, _ ->
        listOf(1) + r.windowed(2).map { it.sum() } + listOf(1)
      }