You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
838 B

1 year ago
package main
import "golang.org/x/exp/constraints"
type Book struct {
Weight uint
Value uint
}
func MaxValue(capacity uint, books []Book) uint {
if capacity == 0 {
return 0
}
if len(books) == 0 {
return 0
}
book := books[0]
1 year ago
withBook := book.Value + MaxValue(capacity-book.Weight, books[1:])
withoutBook := MaxValue(capacity, books[1:])
if book.Weight > capacity {
return withoutBook
}
1 year ago
1 year ago
return Max(withBook, withoutBook)
1 year ago
}
func main() {
println("Did it")
}
func Max[T constraints.Ordered](args ...T) T {
if len(args) == 0 {
return *new(T) // zero value of T
}
if isNan(args[0]) {
return args[0]
}
max := args[0]
for _, arg := range args[1:] {
if isNan(arg) {
return arg
}
if arg > max {
max = arg
}
}
return max
}
func isNan[T constraints.Ordered](arg T) bool {
return arg != arg
}