-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
33 lines (28 loc) · 781 Bytes
/
Copy pathfibonacci.cpp
File metadata and controls
33 lines (28 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
long long int func(vector<long long int>&dp,long long int n)
{
if(n<=1) return n;
if(dp[n]!=-1) return dp[n];
return dp[n]=(func(dp,n-1)+func(dp,n-2))%1000000007;
}
long long int nthFibonacci(long long int n){
// code here
vector<long long int> dp(n+1,-1);
func( dp, n);
return dp[n]%1000000007;
}
//TC: O(n)
//SC: O(n)+O(n) for stack
/************** OPTIMIZE space complexity *******************/
long long int nthFibonacci(long long int n){
// code here
vector<long long int> dp(n+1,-1);
dp[0]=0;
dp[1]=1;
for(int i=2;i<=n;i++)
{
dp[i]=dp[i-1]+dp[i-2]%1000000007;
}
return dp[n]%1000000007;
}
//TC: O(n)
//SC: O(n)