DEV Community

Karleb
Karleb

Posted on

#1137. N-th Tribonacci Number

https://leetcode.com/problems/n-th-tribonacci-number/description/?envType=daily-question&envId=2024-04-24

/**
 * @param {number} n
 * @return {number}
 */
var tribonacci = function (n) {
    if (n < 2) return n

    let dp = [0, 1, 1]

    for (let i = 3; i <= n; i++) {
        dp[i] = dp[i - 3] + dp[i - 2] + dp[i - 1]
    }

    return dp[n]
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)