Skip to content

Commit 6528ac2

Browse files
Merge pull request neetcode-gh#1897 from josuebrunel/feat/0322-coin-change.go
Create 0322-coin-change.go
2 parents de72b80 + ed9f3f2 commit 6528ac2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

go/0322-coin-change.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)