# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/algo/dynamic-programming/n-th-tribonacci-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
