Skip to content

Commit

Permalink
feat: add coin change problem
Browse files Browse the repository at this point in the history
  • Loading branch information
leometzger committed Aug 23, 2023
1 parent 3ced092 commit a131613
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dp/coin_change_problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dp

func getWays(n int64, coins []int64) int64 {
ways := make([]int64, n+1)
ways[0] = 1

for _, coin := range coins {
var i int64
for i = coin; i <= n; i++ {
ways[i] += ways[i-coin]
}
}

return ways[n]
}
37 changes: 37 additions & 0 deletions dp/coin_change_problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dp

import (
"testing"

"github.com/stretchr/testify/assert"
)

type CoinChangeTestCase struct {
n int64
c []int64
result int64
}

func TestCoinChangeProblem(t *testing.T) {
tests := []CoinChangeTestCase{
{
n: 3,
c: []int64{8, 3, 1, 2},
result: 3,
},
{
n: 4,
c: []int64{1, 2, 3},
result: 4,
},
{
n: 10,
c: []int64{2, 5, 3, 6},
result: 5,
},
}

for _, test := range tests {
assert.Equal(t, test.result, getWays(test.n, test.c))
}
}

0 comments on commit a131613

Please sign in to comment.