1128. N-th Tribonacci — Space Optimized

EasyDynamic Programming1D DP

Compute the nth Tribonacci number where T(0)=0, T(1)=1, T(2)=1, and T(n)=T(n-1)+T(n-2)+T(n-3), using only constant extra space. The input is JSON {n}.

Input: JSON {n}.

Output: Integer — the nth Tribonacci number.

Examples

Example 1
Input: {"n":4}
Output: 4
Explanation: 0,1,1,2,4.
Example 2
Input: {"n":25}
Output: 1389537
Explanation: T(25) = 1389537.
Example 3
Input: {"n":0}
Output: 0
Explanation: Base case.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →