fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int nthFibonacciUtil(int n, vector<int>& memo) {
  5. if (n <= 1) {
  6. return n;
  7. }
  8. if (memo[n] != -1) {
  9. return memo[n];
  10. }
  11. memo[n] = nthFibonacciUtil(n - 1, memo)
  12. + nthFibonacciUtil(n - 2, memo);
  13. return memo[n];
  14. }
  15.  
  16. int nthFibonacci(int n) {
  17. vector<int> memo(n + 1, -1);
  18. return nthFibonacciUtil(n, memo);
  19. }
  20.  
  21. int main() {
  22. int n = 5;
  23. int result = nthFibonacci(n);
  24. cout << result << endl;
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
5