https://leetcode.com/problems/fibonacci-number/
dynamic programming day 1
class Solution {
public:
int fib(int n) {
if (n < 2)
return n;
return fib(n - 1) + fib(n - 2);
}
};
class Solution {
public:
int fib(int n) {
int f[n+1];
f[0]=0;
if(n==0){
return 0;
}
f[1]=1;
for(int i=2;i<=n;i++){
f[i] = f[i-1]+f[i-2];
}
return f[n];
}
};
728x90
반응형
'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글
1873. Calculate Special Bonus (0) | 2022.05.30 |
---|---|
1137. N-th Tribonacci Number (0) | 2022.05.27 |
[MySQL] left join, left outer join (0) | 2022.05.27 |
[MySQL] null-safe equal <=>, = (0) | 2022.05.27 |
[MySQL] 같지 않다 (0) | 2022.05.27 |