-
Notifications
You must be signed in to change notification settings - Fork 2
/
coin-change.go
58 lines (51 loc) · 1.27 KB
/
coin-change.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package optimalization
import (
"math"
"sort"
)
// https://leetcode-cn.com/problems/coin-change/
// dynamic programming
func coinChange(coins []int, amount int) int {
dp := make([]int, amount+1)
// init condition
dp[0] = 0
for money := 1; money < len(dp); money++ {
dp[money] = -1
}
for money := 1; money < len(dp); money++ {
dp[money] = math.MaxInt64
for _, c := range coins {
if money-c >= 0 && dp[money-c] != -1 {
dp[money] = min(dp[money], dp[money-c]+1)
}
}
if dp[money] == math.MaxInt64 {
// restore to -1
dp[money] = -1
}
}
return dp[amount]
}
// If greedy policy is used, every time I pick a largest coin, it may not get the final result.
// For example:
// coins: [3, 8], amount: 9
// If you first pick 8, then 3, you cannot get the answer. Actual, it's 3 + 3 + 3.
// error solution
func coinChangeGreedyPolicy(coins []int, amount int) int {
count := 0
sort.Sort(IntSlice(coins))
for i := len(coins) - 1; i >= 0; i-- {
for amount >= coins[i] {
amount -= coins[i]
count++
}
}
if amount != 0 {
count = -1
}
return count
}
type IntSlice []int
func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }