N-th Tribonacci Number LeetCode solution in JavaScript
1// Recursive and Memoiztion approach2var tribonacci = function (n, cache = {}) {3if (n in cache) return cache[n];45//Start of Base Cases6if (n == 0) return 0;7if (n == 1 || n == 2) return 1;8// End Of Base Cases910// Caching the result11cache[n] =12tribonacci(n - 1, cache) +13tribonacci(n - 2, cache) +14tribonacci(n - 3, cache);1516return cache[n];17};1819// Tabulation Approach20var tribonacci = function (n) {21// Create the Table;22const table = Array(n + 1).fill(0);23table[1] = 1;24for (let i = 0; i <= n; i++) {25//Iterate over the table and increase the values respectively26table[i + 1] += table[i];27table[i + 2] += table[i];28table[i + 3] += table[i];29}30return table[n];31};