Created
November 12, 2021 06:54
-
-
Save lior-amsalem/39c162f014a81c9a95cb66e2de512111 to your computer and use it in GitHub Desktop.
codility cache fibonacci to improve performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cache = {}; | |
var yourself = { | |
fibonacci : function(n) { | |
if (n === 0) { | |
return 0; | |
} else if (n === 1) { | |
return 1; | |
} else { | |
let r = cache[n] = cache[n] ? cache[n] : this.fibonacci(n - 1) + | |
this.fibonacci(n - 2); | |
return r; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment