Tribonacci Number in O(n) Time and O(1) Space using JavaScript
The Tribonacci numbers, commonly denoted T(n) form a sequence, called the Tribonacci sequence, such that each number is the sum of the two preceding ones, starting from 0, 1 and 1. That is,
T(0) = 0, T(1) = 1, T(2) = 1 T(n) = T(n - 1) + T(n - 2) + T(n - 3), for n > 2.
Given n, calculate and return T(n).
Example 1:
Input: n = 3 Output: 2 Explanation: T(3) = T(2) + T(1) + T(0) = 1 + 1 + 0 = 2.
Example 2:
Input: n = 4 Output: 4 Explanation: T(4) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4.
Example 3:
Input: n = 5 Output: 7 Explanation: T(5) = T(4) + T(3) + T(2) = 4 + 2 + 1 = 7.
Note:
Your algorithm should run in O(n) time and use O(1) space.
Understanding the Problem
The core challenge of this problem is to compute the nth Tribonacci number efficiently. The Tribonacci sequence is similar to the Fibonacci sequence but sums the last three numbers instead of the last two. This sequence has applications in various mathematical and computational problems.
Approach
To solve this problem, we can start with a naive recursive solution, but it will be highly inefficient due to repeated calculations. Instead, we can use an iterative approach to achieve O(n) time complexity and O(1) space complexity.
Naive Recursive Solution
The naive approach involves a simple recursive function:
function tribonacci(n) {
if (n === 0) return 0;
if (n === 1 || n === 2) return 1;
return tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3);
}
However, this approach has exponential time complexity, O(3^n), due to repeated calculations.
Optimized Iterative Solution
We can optimize the solution by using an iterative approach with three variables to keep track of the last three Tribonacci numbers. This approach ensures O(n) time complexity and O(1) space complexity.
Algorithm
1. Initialize three variables to store the first three Tribonacci numbers: T0 = 0, T1 = 1, T2 = 1.
2. Iterate from 3 to n, updating the variables to store the current Tribonacci number.
3. Return the nth Tribonacci number.
Code Implementation
function tribonacci(n) {
// Base cases
if (n === 0) return 0;
if (n === 1 || n === 2) return 1;
// Initialize the first three Tribonacci numbers
let T0 = 0, T1 = 1, T2 = 1;
// Iterate from 3 to n
for (let i = 3; i <= n; i++) {
// Calculate the next Tribonacci number
let Tn = T0 + T1 + T2;
// Update the variables
T0 = T1;
T1 = T2;
T2 = Tn;
}
// Return the nth Tribonacci number
return T2;
}
Complexity Analysis
The time complexity of the iterative solution is O(n) because we iterate from 3 to n. The space complexity is O(1) because we use a constant amount of space to store the last three Tribonacci numbers.
Edge Cases
1. n = 0: The function should return 0.
2. n = 1 or n = 2: The function should return 1.
3. Large values of n: The function should handle large values efficiently due to its O(n) time complexity.
Testing
To test the solution comprehensively, we can use a variety of test cases:
console.log(tribonacci(0)); // 0
console.log(tribonacci(1)); // 1
console.log(tribonacci(2)); // 1
console.log(tribonacci(3)); // 2
console.log(tribonacci(4)); // 4
console.log(tribonacci(5)); // 7
console.log(tribonacci(25)); // 1389537
Thinking and Problem-Solving Tips
1. Break down the problem into smaller parts and understand the base cases.
2. Consider both recursive and iterative approaches and analyze their time and space complexities.
3. Practice solving similar problems to improve problem-solving skills.
Conclusion
Understanding and solving the Tribonacci number problem helps in developing efficient algorithms for sequence-based problems. The iterative approach ensures optimal time and space complexity, making it suitable for large inputs.
Additional Resources
1. Generalizations of Fibonacci numbers