EasyDynamic ProgrammingRecursionMemoizationMath

N-th Tribonacci Number

LeetCode
2 approaches, code in all languages

The Tribonacci sequence is seeded with T(0) = 0, T(1) = 1, and T(2) = 1. From there on, every term is the sum of the three that come right before it, so T(n) = T(n - 1) + T(n - 2) + T(n - 3) for any n greater than 2.

Given a non-negative index n, return the value of T(n).

Example 1

Input: n = 4

Output: 4

T(3) = 0 + 1 + 1 = 2, and then T(4) = 1 + 1 + 2 = 4.

Example 2

Input: n = 25

Output: 1389537

Constraints

  • 0 ≤ n ≤ 37
  • The answer is guaranteed to fit inside a signed 32-bit integer (at most 2^31 − 1).
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now