Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day384 #777

Merged
merged 2 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,6 @@ problems from
* [Day 381](https://github.com/vaskoz/dailycodingproblem-go/issues/768)
* [Day 382](https://github.com/vaskoz/dailycodingproblem-go/issues/770)
* [Day 383](https://github.com/vaskoz/dailycodingproblem-go/issues/772)
* [Day 384](https://github.com/vaskoz/dailycodingproblem-go/issues/776)
* [Day 385](https://github.com/vaskoz/dailycodingproblem-go/issues/774)

44 changes: 44 additions & 0 deletions day384/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package day384

import (
"errors"
"sort"
)

// nolint
var errImpossibleTarget = errors.New("impossible to make target change with denominations")

const MaxInt = int(^uint(0) >> 1)

// FewestCoinsBrute returns the fewest required coins to make target
// in change.
// Returns an error if it's not possible.
func FewestCoinsBrute(denom []int, target int) (int, error) {
copied := append([]int{}, denom...)
sort.Sort(sort.Reverse(sort.IntSlice(copied)))

return fewestCoinsBrute(copied, target, MaxInt)
}

func fewestCoinsBrute(denom []int, target, coinsSoFar int) (int, error) {
if target < 0 || coinsSoFar < 0 {
return 0, errImpossibleTarget
} else if target == 0 {
return 0, nil
}

smallest := MaxInt

for _, d := range denom {
if res, err := fewestCoinsBrute(denom, target-d, coinsSoFar-1); err == nil && res < smallest {
smallest = res
coinsSoFar = res
}
}

if smallest == MaxInt {
return 0, errImpossibleTarget
}

return 1 + smallest, nil
}
33 changes: 33 additions & 0 deletions day384/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day384

import "testing"

// nolint
var testcases = []struct {
denom []int
target int
expectedCoins int
expectedErr error
}{
{[]int{1, 5, 10}, 56, 7, nil},
{[]int{5, 8}, 15, 3, nil},
{[]int{5, 10}, 2, 0, errImpossibleTarget},
}

func TestFewestCoinsBrute(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if res, err := FewestCoinsBrute(tc.denom, tc.target); res != tc.expectedCoins || err != tc.expectedErr {
t.Errorf("Expected (%v,%v), got (%v,%v)", tc.expectedCoins, tc.expectedErr, res, err)
}
}
}

func BenchmarkFewestCoinsBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
FewestCoinsBrute(tc.denom, tc.target) // nolint
}
}
}