LeetCode - 509.Fibonacci Num & 1137. N-th Tribonacci Number

2022. 7. 25. 21:20STUDY/LeetCode

반응형

Fibonacci 와 Tribonacci 이다. 

푸는 방법은 똑같지롱.

Fibonacci 

int dp[100] = {0,};
int fib(int n){
    if(n == 0) return 0;
    else if(n == 1) return 1;
    
    if(dp[n] > 0) return dp[n];
    else {
        dp[n] = fib(n-1) + fib(n-2);
    }
    return dp[n];
}

Tribonacci

int dp[100] = {0,};
int tribonacci(int n)
{
	if(n == 0) return 0;
    else if(n == 1|| n == 2) return 1;
    
    if(dp[n] > 0) return dp[n];
    else {
    	dp[n] = tribonacci(n-3) + tribonacci(n-2) + tribonacci(n-1);    	
    }
    return dp[n];
}

 

728x90
반응형

'STUDY > LeetCode' 카테고리의 다른 글

1678. Goal Parser Interpretation (C/C++)  (2) 2022.08.04
53. Maximum Subarray  (1) 2022.07.31
LeetCode - 240. Search a 2D Matrix II  (0) 2022.07.24
LeetCode - 733. Flood Fill  (0) 2022.07.17
LeetCode - 19. Remove Nth Node From End of List  (1) 2022.07.16