Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. const target = 200
  6.  
  7. var denoms = [...]int{200, 100, 50, 20, 10, 5, 2, 1}
  8.  
  9. func main() {
  10. fmt.Println(CoinSum(0, 0, make(map[string]int)))
  11. }
  12.  
  13. func CoinSum(sum int, index int, memory map[string]int) (result int) {
  14. if index >= len(denoms) || sum > target {
  15. return 0
  16. } else if sum == target {
  17. return 1
  18. }
  19. key := fmt.Sprint(sum, index)
  20. if result, ok := memory[key]; ok {
  21. return result
  22. }
  23. result += CoinSum(sum+denoms[index], index, memory)
  24. result += CoinSum(sum, index+1, memory)
  25. memory[key] = result
  26. return
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement