main
Jason Staten 9 months ago
parent 5bfc228c92
commit 75634e210c

@ -16,11 +16,13 @@ func MaxValue(capacity uint, books []Book) uint {
}
book := books[0]
return Max(
book.Value+MaxValue(capacity-book.Weight, books[1:]),
MaxValue(capacity, books[1:]),
)
withBook := book.Value + MaxValue(capacity-book.Weight, books[1:])
withoutBook := MaxValue(capacity, books[1:])
if book.Weight > capacity {
return withoutBook
}
return Max(withBook, withoutBook)
}
func main() {

@ -35,3 +35,11 @@ func TestPrefersMultiple(t *testing.T) {
})
be.Equal(t, 6, result)
}
func TestIgnoresTooBig(t *testing.T) {
result := ks.MaxValue(1, []ks.Book{
{Weight: 10, Value: 4},
{Weight: 1, Value: 1},
})
be.Equal(t, 1, result)
}

Loading…
Cancel
Save