N-th Tribonacci Number

Problem Id: 1137 Difficulty: Easy


Intuition

Solution


class Solution(object):
    def tribonacci(self, n):
        """
        :type n: int
        :rtype: int
        """
        a1 = 0
        a2 = 1
        a3 = 1
        if n == 0:
            return 0
        elif n == 1:
            return 1
        elif n == 2:
            return 1
        while n >= 3:
            n -= 1
            a1, a2, a3 = a2, a3, (a1 + a2 + a3)
        return a3