LeetCode Entry
1137. N-th Tribonacci Number
another way is to use dp cache
1137. N-th Tribonacci Number easy
fun tribonacci(n: Int): Int = if (n < 2) n else {
var t0 = 0
var t1 = 1
var t2 = 1
repeat(n - 2) {
t2 += (t0 + t1).also {
t0 = t1
t1 = t2
}
}
t2
}
Telegram
https://t.me/leetcode_daily_unstoppable/102
Intuition
Just do what is asked.
Approach
- another way is to use dp cache
Complexity
- Time complexity: \(O(n)\)
- Space complexity: \(O(1)\)