Advertisement
vaibhav1906

Memoization code for Fibonacci Number

Nov 24th, 2021
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int solve(vector<int> &dp,int x)
  4.     {
  5.         if(dp[x]!=-1)
  6.             return dp[x];
  7.         else if(x<=1)
  8.         {
  9.             dp[x]=x;
  10.             return dp[x];
  11.         }
  12.         dp[x]=solve(dp,x-1)+solve(dp,x-2);
  13.         return dp[x];
  14.     }
  15.     int fib(int n) {
  16.         vector<int> dp(n+1,-1);
  17.         return solve(dp,n);
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement