-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ced092
commit a131613
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |