N-th Tribonacci Number - Problem
The Tribonacci sequence is a fascinating extension of the famous Fibonacci sequence! While Fibonacci numbers are formed by adding the previous two numbers, Tribonacci numbers are formed by adding the previous three numbers.
The sequence is defined as:
T0 = 0T1 = 1T2 = 1Tn = Tn-3 + Tn-2 + Tn-1forn โฅ 3
For example: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81...
Goal: Given an integer n, return the n-th Tribonacci number Tn.
Input & Output
example_1.py โ Python
$
Input:
n = 4
โบ
Output:
4
๐ก Note:
T(4) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4
example_2.py โ Python
$
Input:
n = 25
โบ
Output:
1389537
๐ก Note:
The 25th Tribonacci number, calculated efficiently using the sliding window approach
example_3.py โ Python
$
Input:
n = 0
โบ
Output:
0
๐ก Note:
Base case: T(0) = 0 by definition
Constraints
- 0 โค n โค 37
- Answer is guaranteed to fit within a 32-bit integer
- The sequence grows exponentially, similar to Fibonacci
Visualization
Tap to expand
Understanding the Visualization
1
Foundation
Start with floors 0, 1, 2 having heights 0, 1, 1
2
Build Up
Each new floor = sum of previous 3 floors
3
Slide Window
Keep only the last 3 floor heights in memory
4
Reach Target
Continue until we reach the desired floor n
Key Takeaway
๐ฏ Key Insight: The sliding window approach with three variables gives us optimal O(n) time and O(1) space complexity, making it the perfect balance of simplicity and efficiency!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code