Created
June 30, 2018 10:35
-
-
Save desigens/f44833a4700fe79370ab20e0e896212f to your computer and use it in GitHub Desktop.
Codility
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
var yourself = { | |
cache: {}, | |
fibonacci : function(n) { | |
if (n === 0) { | |
return 0; | |
} else if (n === 1) { | |
return 1; | |
} else { | |
let a = this.cache[n - 1]; | |
if (a === undefined) { | |
a = this.fibonacci(n - 1); | |
this.cache[n - 1] = a; | |
} | |
let b = this.cache[n - 2]; | |
if (b === undefined) { | |
b = this.fibonacci(n - 2); | |
this.cache[n - 2] = b; | |
} | |
return a + b; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment