> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/dynamic-programming/n-th-tribonacci-number.md).

# N-th Tribonacci Number

{% embed url="<https://leetcode.com/problems/n-th-tribonacci-number>" %}

## Solution 1

```java
class Solution {
    public int tribonacci(int n) {
        if (n <= 1) return n;
        if (n == 2) return 1;
        
        int a = 0, b = 1, c = 1, d = 2;
        
        for (int i = 4; i < n + 1; i++) {
            a = b;
            b = c;
            c = d;
            d = a + b + c;
        }
        return d;
    }
}

// a b c d
//   a b c d
//     a b c d
//       a b c d
// 0 1 1 2 4 7 13
```

This is the same bottom-up approach used in [Fibonacci Number](/algo/dynamic-programming/fibonacci-number.md#solution-3). The only difference here is that we use 4 variables to keep the results.
