N-th Tribonacci Number - Complete Solution Guide
N-th Tribonacci Number is LeetCode problem 1137, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
The Tribonacci sequence T n is defined as follows: T 0 = 0, T 1 = 1, T 2 = 1, and T n+3 = T n + T n+1 + T n+2 for n >= 0. Given n , return the value of T n . Example 1: Input: n = 4 Output: 4 Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4 Example 2: Input: n = 25 Output: 1389537 Constraints: 0 <= n <= 37 The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1 .
Detailed Explanation
The "N-th Tribonacci Number" problem challenges us to generate a specific term from the Tribonacci sequence, a close cousin to the more famous Fibonacci sequence. Instead of each number being the sum of the two preceding ones, a Tribonacci number is the sum of the *three* preceding numbers. The problem provides the fundamental building blocks: `T_0 = 0`, `T_1 = 1`, and `T_2 = 1`. From `n >= 0`, the sequence then proceeds with `T_{n+3} = T_n + T_{n+1} + T_{n+2}`. Our task is to calculate and return `T_n` for a given input `n`.
Solution Approach
The provided solution leverages an iterative, bottom-up dynamic programming strategy to efficiently compute the N-th Tribonacci number. Instead of blindly following the recursive definition, which would cause redundant computations, this approach systematically builds up the sequence from its base cases. It handles the initial `n=0`, `n=1`, `n=2` cases directly. For `n >= 3`, it initializes an array (or list in Python) `trib` with the first three Tribonacci numbers: `[0, 1, 1]`, corresponding to `T_0`, `T_1`, and `T_2` respectively. The core insight here is that to calculate `T_i`, we only need the three immediately preceding values: `T_{i-1}, T_{i-2}, T_{i-3}`. Since we're building the sequence *up* from `T_0`, these preceding values will always have already been computed and stored in our `trib` array. The solution then simply iterates from `i = 3` up to `n`. In each step, it appends the sum of `trib[i-1] + trib[i-2] + trib[i-3]` to the `trib` array, effectively calculating `T_i`. This guarantees that each Tribonacci number is computed exactly once. Once the loop finishes, `trib[n]` holds the desired N-th Tribonacci number, which is then returned. This elegant method transforms a potentially exponential problem into a linear one by remembering past results.
Step-by-Step Algorithm
- Handle base cases: If n is 0, return 0. If n is 1 or 2, return 1.
- Initialize: Set the first three Tribonacci numbers (T<sub>0</sub>, T<sub>1</sub>, T<sub>2</sub>). This can be done using an array or individual variables.
- Iterate: Loop from 3 to n (inclusive). In each iteration, calculate the next Tribonacci number by summing the three previous numbers.
- Update: Update the variables (or array) to store the newly calculated Tribonacci number and shift older values.
- Return: After the loop completes, return the n-th Tribonacci number.
Key Insights
- **Recursive Definition Implies Dynamic Programming:** The problem's definition `T_n = T_{n-1} + T_{n-2} + T_{n-3}` immediately reveals an optimal substructure and overlapping subproblems. This pattern is a canonical signal for dynamic programming, where storing previously computed values prevents redundant calculations and improves efficiency dramatically over naive recursion.
- **Precise Handling of Base Cases:** Getting the initial conditions `T_0 = 0, T_1 = 1, T_2 = 1` absolutely correct is foundational. Any error or omission in these base cases will propagate throughout the entire sequence, leading to an incorrect `T_n`. The solution explicitly handles `n=0`, `n=1`, and `n=2` before entering the iterative loop, ensuring the sequence starts correctly.
- **Iterative Bottom-Up Build-up for O(n) Efficiency:** The solution's iterative approach, starting from the known base cases and incrementally calculating `T_i` up to `T_n`, ensures each Tribonacci number is computed in constant time (O(1)) and exactly once. This leads to an overall time complexity of O(n) and space complexity of O(n) (to store the sequence), which is highly efficient and perfectly suited for the given constraints of `n <= 37`.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Math, Dynamic Programming, Memoization.
Companies
Asked at: Accenture, Coursera, tcs.