mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 11:46:28 +08:00
51 lines
1.2 KiB
Go
51 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]
|
||
|
}
|