Created
February 5, 2019 07:50
-
-
Save stevencurtis/8e8dc1ad79e2960fe637d80acf409f7d to your computer and use it in GitHub Desktop.
Fibonacci bottom-up
This file contains 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
func fib(_ n: Int) -> Int { | |
guard n > 1 else { return n } | |
var cache : [Int] = Array(repeating: 0, count: n) | |
cache[0] = 0 | |
cache[1] = 1 | |
for i in 2..<n { | |
cache[i] = cache[i - 1] + cache[i - 2] | |
} | |
return cache[n - 1] + cache[n - 2] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment