Leetcode Note: Go - N-Th Tribonacci Number

N-th Tribonacci Number - LeetCode
https://leetcode.com/problems/n-th-tribonacci-number/

  • Go 言語で取り組んだメモ

回答

Go. Constant space O(1). Non-recursive - LeetCode Discuss
https://leetcode.com/problems/n-th-tribonacci-number/discuss/569763/Go.-Constant-space-O(1).-Non-recursive

func tribonacci(n int) int {
    triset := [3]int{0, 1, 1}
    if n < 3 {
        return triset[n]
    }
    for i := 3; i <= n; i++ {
        triset[i % 3] = triset[0] + triset[1] + triset[2]
    }
    return triset[n % 3]
}