Skip to content

Instantly share code, notes, and snippets.

@jhorowitz
Last active February 14, 2016 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhorowitz/18583a050eb78d14977b to your computer and use it in GitHub Desktop.
Save jhorowitz/18583a050eb78d14977b to your computer and use it in GitHub Desktop.
Recursive solution for Coin Sum DP Problem
package main
import "fmt"
const target = 200
var denoms = [...]int{200, 100, 50, 20, 10, 5, 2, 1}
func main() {
fmt.Println(CoinSum(0, 0, make(map[string]int)))
}
func CoinSum(sum int, index int, memory map[string]int) (result int) {
if index >= len(denoms) || sum > target {
return 0
} else if sum == target {
return 1
}
key := fmt.Sprint(sum, index)
if result, ok := memory[key]; ok {
return result
}
result += CoinSum(sum+denoms[index], index, memory)
result += CoinSum(sum, index+1, memory)
memory[key] = result
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment