We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents de72b80 + ed9f3f2 commit 6528ac2Copy full SHA for 6528ac2
go/0322-coin-change.go
@@ -0,0 +1,26 @@
1
+func coinChange(coins []int, amount int) int {
2
+ changes := make([]int, amount+1)
3
+ for i := range changes {
4
+ changes[i] = amount + 1
5
+ }
6
+ changes[0] = 0
7
+
8
+ for i := 1; i < amount+1; i++ {
9
+ for _, c := range coins {
10
+ if c <= i {
11
+ changes[i] = min(changes[i], 1+changes[i-c])
12
13
14
15
+ if changes[amount] != amount+1 {
16
+ return changes[amount]
17
18
+ return -1
19
+}
20
21
+func min(x, y int) int {
22
+ if x < y {
23
+ return x
24
25
+ return y
26
0 commit comments