#1137

N-th Tribonacci Number

Easy 1D DP

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

T0 = 0, T1 = 1, T2 = 1 and each later term is the sum of the three before it. Return Tn.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: each term is the sum of the previous three.

Why this works: keep the last three values and roll forward.

Example: 0, 1, 1, 2, 4, 7 → T5 = 7

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the base cases for n = 0, 1 and 2.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon