Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1897 from josuebrunel/feat/0322-coin-c…
Browse files Browse the repository at this point in the history
…hange.go

Create 0322-coin-change.go
  • Loading branch information
AP-Repositories authored Jan 5, 2023
2 parents de72b80 + ed9f3f2 commit 6528ac2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions go/0322-coin-change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
func coinChange(coins []int, amount int) int {
changes := make([]int, amount+1)
for i := range changes {
changes[i] = amount + 1
}
changes[0] = 0

for i := 1; i < amount+1; i++ {
for _, c := range coins {
if c <= i {
changes[i] = min(changes[i], 1+changes[i-c])
}
}
}
if changes[amount] != amount+1 {
return changes[amount]
}
return -1
}

func min(x, y int) int {
if x < y {
return x
}
return y
}

0 comments on commit 6528ac2

Please sign in to comment.