Home >> Programming >> N-th Tribonacci Number Hey there, welcome back to another post. In this post, we will learn how to solve the N-th Tribonacci Number problem and will implement its solution in Java. N-th Tribonacci Number Problem Statement The Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. Given n, return the value of Tn. Example 1: Input: n = 4 Output: 4 Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4 N-th Tribonacci Number Java Solution using Bottom-Up DP This problem is similar to the Fibonacci series. We can solve it using the Bottom-Up approach of dynamic programming. Time Complexity: O(n) Space Complexity: O(n) The space complexity can be reduced to O(1) by using variables to store the previous three values instead of using an array. Java Program: class Solution { public int tribonacci( int n) { if (n == 0) return 0; ...
Comments
Post a Comment