Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. // Time complexity: O(n)
  2. // Space complexity: O(n)
  3.  
  4. const memo = new Map();
  5.  
  6. function fibonacci(n) {
  7. if (memo.has(n)) {
  8. return memo.get(n);
  9. }
  10.  
  11. if (n <= 1) {
  12. return 1;
  13. }
  14.  
  15. const result = fibonacci(n - 1) + fibonacci(n - 2);
  16.  
  17. memo.set(n, result);
  18.  
  19. return result;
  20. }
  21.  
  22. const array = [];
  23.  
  24. for (let i = 0; i < 10; i++) {
  25. array.push(fibonacci(i));
  26. }
  27.  
  28. console.log(array);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement