Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Last active August 29, 2015 14:23
Show Gist options
  • Save codetalks-new/b356f673e6de22865c94 to your computer and use it in GitHub Desktop.
Save codetalks-new/b356f673e6de22865c94 to your computer and use it in GitHub Desktop.
var cache = [Int:Int]()
func numTrees(n:Int) -> Int{
if n < 2{
return 1
}
if n == 2{
return 2
}
if let value = cache[n]{
return value
}
var count = 0
for m in 0..<n {
count += numTrees(m) * numTrees(n - m - 1)
}
cache[n] = count
return count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment