oneby7

1-D Dynamic Programming Take it or skip it

520. N-th Tribonacci Number

Easy

The Tribonacci sequence starts with the three terms 0, 1 and 1. Every later term is the sum of the three terms just before it: 0, 1, 1, 2, 4, 7, 13, 24, and so on.

T(n) is the term at index n, counting from 0:

  • T(0) = 0
  • T(1) = 1
  • T(2) = 1
  • T(3) = T(2) + T(1) + T(0) = 1 + 1 + 0 = 2
  • T(4) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4

Given an integer n, return T(n).

Example 1

Input: n = 7

Output: 24

Explanation: The terms at indexes 0 to 7 are 0, 1, 1, 2, 4, 7, 13 and 24. The last one, 24, is 4 + 7 + 13.

Example 2

Input: n = 0

Output: 0

Explanation: The sequence starts at index 0, and its first term is 0.

Example 3

Input: n = 37

Output: 2082876103

Check your understanding

Work out the answer for this input before you write any code.

n = 8

Constraints

  • 0 ≤ n ≤ 37

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.