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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 | func coinChange(coins []int, amount int) int {
// return func1(coins, amount);
// return func2(coins, amount);
// return completeKnapsack4(coins, amount);
// return completeKnapsack5(coins, amount);
return completeKnapsack6(coins, amount);
}
// 1. Basic, Time Limit Exceeded
func func1(coins []int, amount int) int {
if amount == 0 {
return 0;
}
ret := -1;
for _, coin := range coins {
if coin == amount {
return 1;
}
if coin < amount {
tmp := func1(coins, amount - coin);
if tmp != -1 {
if ret == -1 {
ret = 1 + tmp;
} else if ret > tmp + 1 {
ret = tmp + 1;
}
}
}
}
return ret;
}
// 2. Top-down memo, Time Limit Exceeded
func func2(coins []int, amount int) int {
memo := make(map[int]int);
return helper2(coins, amount, memo);
}
func helper2(coins []int, amount int, memo map[int]int) int {
if amount == 0 {
return 0;
}
if val, ok := memo[amount]; ok {
return val;
}
ret := -1;
for _, coin := range coins {
if coin == amount {
ret = 1;
break;
}
if coin < amount {
tmp := helper2(coins, amount - coin, memo);
if tmp != -1 {
if ret == -1 {
ret = 1 + tmp;
} else if ret > tmp + 1 {
ret = tmp + 1;
}
}
}
}
memo[amount] = ret;
return memo[amount];
}
// 3. Bottom-up??
// 4. Complete knapsack, 2D array, -1 as failed condition => complex checking
func completeKnapsack4(coins []int, amount int) int {
if amount == 0 {
return 0;
}
N := len(coins);
V := amount;
pack := make([][]int, N+1);
for i := 0; i < N+1; i++ {
pack[i] = make([]int, V+1);
for j := 0; j < V+1; j++ {
pack[i][j] = -1;
}
}
for i := 0; i < N+1; i++ {
pack[i][0] = 0;
}
for i := 1; i <= N; i++ {
for v := 0; v <= V; v++ {
if pack[i-1][v] != -1 {
pack[i][v] = pack[i-1][v];
}
if v >= coins[i-1] && pack[i][v-coins[i-1]] != -1 {
if pack[i][v] == -1 {
pack[i][v] = pack[i][v-coins[i-1]] + 1;
} else {
pack[i][v] = min(pack[i][v], pack[i][v-coins[i-1]] + 1);
}
}
}
}
ret := -1;
for i := 1; i <= N; i++ {
if pack[i][V] != -1 {
if ret == -1 {
ret = pack[i][V];
} else {
ret = min(ret, pack[i][V]);
}
}
}
return ret;
}
// 5. Complete knapsack, 2D array, amount + 1 as failed condition => simple checking
// !!!Key Point, set ret = amount + 1,
// because max number of operations is put 1 into package with amount time
func completeKnapsack5(coins []int, amount int) int {
if amount == 0 {
return 0;
}
N := len(coins);
V := amount;
pack := make([][]int, N+1);
for i := 0; i < N+1; i++ {
pack[i] = make([]int, V+1);
for j := 0; j < V+1; j++ {
pack[i][j] = amount + 1;
}
}
for i := 0; i < N+1; i++ {
pack[i][0] = 0;
}
for i := 1; i <= N; i++ {
for v := 0; v <= V; v++ {
pack[i][v] = pack[i-1][v];
if v >= coins[i-1] {
pack[i][v] = min(pack[i][v], pack[i][v-coins[i-1]] + 1);
}
}
}
ret := amount + 1;
for i := 1; i <= N; i++ {
ret = min(ret, pack[i][V]);
}
if ret == amount + 1 {
return -1;
}
return ret;
}
// 6. Complete knapsack, 1D array
func completeKnapsack6(coins []int, amount int) int {
if amount == 0 {
return 0;
}
N := len(coins);
V := amount;
pack := make([]int, V+1);
for i := 1; i < V+1; i++ {
pack[i] = amount + 1;
}
for i := 1; i <= N; i++ {
for v := 0; v <= V; v++ {
if v >= coins[i-1] {
pack[v] = min(pack[v], pack[v-coins[i-1]] + 1);
}
}
}
if pack[V] == amount + 1 {
return -1;
}
return pack[V];
}
func min(a int, b int) int {
if a < b {
return a;
}
return b;
}
|