Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created February 5, 2019 07:50
Show Gist options
  • Save stevencurtis/8e8dc1ad79e2960fe637d80acf409f7d to your computer and use it in GitHub Desktop.
Save stevencurtis/8e8dc1ad79e2960fe637d80acf409f7d to your computer and use it in GitHub Desktop.
Fibonacci bottom-up
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