Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 643 Bytes

leetcode_1137.md

File metadata and controls

32 lines (24 loc) · 643 Bytes

LeetCode Problems

1137. N-th Tribonacci Number

class Solution {
    fun tribonacci(n: Int): Int {
        // process definitive cases
        if (n == 0) return 0
        if (n <= 2) return 1
        // if not, do case by case for Tribonacci
        val dp = IntArray(n + 1)
        dp[1] = 1
        dp[2] = 1
        // iterate
        for (i in 3 until n + 1) {
            dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
        }
        return dp[n]
    }
}