Last active
February 14, 2016 17:14
-
-
Save jhorowitz/ce429e1452d7c611174d to your computer and use it in GitHub Desktop.
Coin sum problem solved recursively in exponential time.
This file contains hidden or 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
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)) | |
} | |
func CoinSum(sum int, index int) (result int) { | |
if index >= len(denoms) || sum > target { | |
return 0 | |
} else if sum == target { | |
return 1 | |
} | |
result += CoinSum(sum+denoms[index], index) | |
result += CoinSum(sum, index+1) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment