mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 01:36:29 +08:00
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
// File: unbounded_knapsack.go
|
|
// Created Time: 2023-07-23
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_dynamic_programming
|
|
|
|
import "math"
|
|
|
|
/* 完全背包:动态规划 */
|
|
func unboundedKnapsackDP(wgt, val []int, cap int) int {
|
|
n := len(wgt)
|
|
// 初始化 dp 表
|
|
dp := make([][]int, n+1)
|
|
for i := 0; i <= n; i++ {
|
|
dp[i] = make([]int, cap+1)
|
|
}
|
|
// 状态转移
|
|
for i := 1; i <= n; i++ {
|
|
for c := 1; c <= cap; c++ {
|
|
if wgt[i-1] > c {
|
|
// 若超过背包容量,则不选物品 i
|
|
dp[i][c] = dp[i-1][c]
|
|
} else {
|
|
// 不选和选物品 i 这两种方案的较大值
|
|
dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))
|
|
}
|
|
}
|
|
}
|
|
return dp[n][cap]
|
|
}
|
|
|
|
/* 完全背包:空间优化后的动态规划 */
|
|
func unboundedKnapsackDPComp(wgt, val []int, cap int) int {
|
|
n := len(wgt)
|
|
// 初始化 dp 表
|
|
dp := make([]int, cap+1)
|
|
// 状态转移
|
|
for i := 1; i <= n; i++ {
|
|
for c := 1; c <= cap; c++ {
|
|
if wgt[i-1] > c {
|
|
// 若超过背包容量,则不选物品 i
|
|
dp[c] = dp[c]
|
|
} else {
|
|
// 不选和选物品 i 这两种方案的较大值
|
|
dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))
|
|
}
|
|
}
|
|
}
|
|
return dp[cap]
|
|
}
|