N-th Tribonacci Number

ㅋㅋ·2023년 1월 30일
0

알고리즘-leetcode

목록 보기
100/135

목표로 하는 정수 n을 받고 n번째 tribonacci number를 구하는 문제

class Solution {
public:
    int tribonacci(int n) {

        if (n == 0)
        {
            return 0;
        }
        else if (n == 1 || n == 2)
        {
            return 1;
        }
        
        vector<int> table(n + 1);
        table[0] = 0;
        table[1] = 1;
        table[2] = 1;

        for (int i = 3; i <= n; i++)
        {
            table[i] = table[i - 3] + table[i - 2] + table[i - 1];
        }

        return table[n];
    }
};

0개의 댓글