|
| 1 | +package backtracking |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +const intMax = int(^uint(0) >> 1) |
| 8 | + |
| 9 | +var Cnt int |
| 10 | + |
| 11 | +// LeastCoins find least number of coins of which the total values are equals a given one |
| 12 | +func LeastCoins(targetTotal int, coinOptions []int) int { |
| 13 | + minNum := intMax |
| 14 | + |
| 15 | + memo := make([][]int, targetTotal+1) |
| 16 | + for i := range memo { |
| 17 | + memo[i] = make([]int, len(coinOptions)) |
| 18 | + } |
| 19 | + fmt.Println("start") |
| 20 | + leastCoins(&minNum, 0, targetTotal, len(coinOptions)-1, coinOptions, memo) |
| 21 | + fmt.Println("end") |
| 22 | + |
| 23 | + return minNum |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +func leastCoins(minNum *int, cNum, totalValue, opIndex int, coinOptions []int, memo [][]int) { |
| 28 | + Cnt++ |
| 29 | + if 0 == totalValue { |
| 30 | + if cNum < *minNum { |
| 31 | + *minNum = cNum |
| 32 | + } |
| 33 | + |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if opIndex < 0 { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + num4Option := 0 |
| 42 | + remaining := totalValue - coinOptions[opIndex]*num4Option |
| 43 | + for remaining >= 0 { |
| 44 | + |
| 45 | + if opIndex != 0 { |
| 46 | + if shouldSkip(memo, remaining, opIndex-1, cNum+num4Option) { |
| 47 | + goto Next |
| 48 | + } |
| 49 | + } |
| 50 | + leastCoins(minNum, cNum+num4Option, remaining, opIndex-1, coinOptions, memo) |
| 51 | + |
| 52 | + Next: |
| 53 | + num4Option++ |
| 54 | + remaining = totalValue - coinOptions[opIndex]*num4Option |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +func shouldSkip(memo [][]int, totalValue, nextOpIdex, cNum int) bool { |
| 61 | + if memo[totalValue][nextOpIdex] > 0 && memo[totalValue][nextOpIdex] <= cNum { |
| 62 | + fmt.Printf("skip,%d, %d as %d <= %d \n", totalValue, nextOpIdex, memo[totalValue][nextOpIdex], cNum) |
| 63 | + return true |
| 64 | + } |
| 65 | + if memo[totalValue][nextOpIdex] == 0 || memo[totalValue][nextOpIdex] > cNum { |
| 66 | + memo[totalValue][nextOpIdex] = cNum |
| 67 | + } |
| 68 | + return false |
| 69 | +} |
| 70 | + |
| 71 | +func LeastCoins2(targetTotal int, coinOptions []int) int { |
| 72 | + |
| 73 | + minNum := intMax |
| 74 | + memo := make([][]bool, targetTotal) |
| 75 | + for i := range memo { |
| 76 | + memo[i] = make([]bool, targetTotal/coinOptions[0]) |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Println("start") |
| 80 | + leastCoins2(&minNum, targetTotal, coinOptions, 0, 0, memo) |
| 81 | + fmt.Println("end") |
| 82 | + |
| 83 | + return minNum |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +func leastCoins2(minNum *int, targetTotal int, coinOptions []int, cNum, cValue int, memo [][]bool) { |
| 88 | + Cnt++ |
| 89 | + if cValue == targetTotal { |
| 90 | + if *minNum > cNum { |
| 91 | + *minNum = cNum |
| 92 | + } |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + for _, coin := range coinOptions { |
| 97 | + if coin+cValue <= targetTotal && !memo[cValue+coin-1][cNum] { |
| 98 | + memo[cValue+coin-1][cNum] = true |
| 99 | + leastCoins2(minNum, targetTotal, coinOptions, cNum+1, cValue+coin, memo) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments