Advertisement
vaibhav1906

Tabulation code for Fibonacci Number with space optimization

Nov 24th, 2021
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.27 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int fib(int n) {
  4.         if(n<=1)
  5.             return n;
  6.         int a=0;
  7.         int b=1;
  8.         int c;
  9.         for(int i=2;i<=n;i++)
  10.         {
  11.             c=a+b;
  12.             a=b;
  13.             b=c;
  14.         }
  15.         return c;
  16.     }
  17. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement