ZhongKuo0228 / study

0 stars 0 forks source link

1137. N-th Tribonacci Number #115

Open fockspaces opened 11 months ago

fockspaces commented 11 months ago

use tuple with three elements to maintain the relationship. keep updating unitil we find the ans

class Solution:
    def tribonacci(self, n: int) -> int:
        prev = (0, 1, 1)
        if n < 3:
            return prev[n]
        for i in range(2, n):
            prev = prev[1], prev[2], prev[0] + prev[1] + prev[2]
        return prev[2]