1137. N-th Tribonacci Number
N-th Tribonacci Number (LeetCode 1137) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Fibonacci Style) subpattern.
How to solve N-th Tribonacci Number
N-th Tribonacci Number is a Fibonacci Style DP problem, part of Dynamic Programming (DP) Patterns. Solve problems with Fibonacci-like recurrence: climbing stairs, house robber, decode ways.
Work towards the solution in these steps:
- Handle the three initial indices directly.
- Each new value depends only on the previous three values.
- Advance through the sequence from left to right while maintaining those three values.
The reference solution runs in O(n) time and O(1) space.
The N-th Tribonacci Number solution and editorial walks through the full approach with reference code.
Problem statement
The Tribonacci sequence is defined by three initial values: T0 = 0, T1 = 1, and T2 = 1. For every index k >= 3, the next value is the sum of the three preceding values: Tk = T(k - 1) + T(k - 2) + T(k - 3).
Given the integer parameter n, return Tn, the Tribonacci value at index n. The method must return the result as an integer.
Example 1:
Input: n = 2 Output: 1 Explanation: The second initial Tribonacci value is T2 = 1.
Example 2:
Input: n = 6 Output: 13 Explanation: Starting with 0, 1, 1, each later value sums the previous three, giving T6 = 13.
Example 3:
Input: n = 0 Output: 0 Explanation: The sequence is defined with T0 = 0.
Constraints:
- n is an integer. - 0 <= n <= 37. - The answer fits in a 32-bit signed integer.
Topics and companies
Topics: Dynamic Programming, Iteration, Math.
How to practise N-th Tribonacci Number on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open N-th Tribonacci Number in the code editor, or read the N-th Tribonacci Number editorial for a worked solution with its approach and complexity analysis.
Where N-th Tribonacci Number sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- 1D Array (Fibonacci Style) subpattern
- The full DSA patterns sheet
Problems related to N-th Tribonacci Number
Other problems that use the same 1D Array (Fibonacci Style) and Dynamic Programming (DP) Patterns techniques:
- Climbing Stairs — Easy, same subpattern
- Decode Ways — Medium, same subpattern
- Delete and Earn — Medium, same subpattern
- Fibonacci Number — Easy, same subpattern
- House Robber — Medium, same subpattern
- House Robber II — Medium, same subpattern
N-th Tribonacci Number FAQ
What is the LeetCode number of N-th Tribonacci Number?
N-th Tribonacci Number is LeetCode problem 1137, rated Easy.
What pattern is used to solve N-th Tribonacci Number?
N-th Tribonacci Number is a Fibonacci Style DP problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Fibonacci Style) subpattern.
What is the time complexity of N-th Tribonacci Number?
The reference solution runs in O(n) time and O(1) space.