diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 3ec399f1..54e772e0 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -340,6 +340,27 @@ function test_completePack2() { } ``` +TypeScript: + +```typescript +// 先遍历物品,再遍历背包容量 +function test_CompletePack(): void { + const weight: number[] = [1, 3, 4]; + const value: number[] = [15, 20, 30]; + const bagSize: number = 4; + const dp: number[] = new Array(bagSize + 1).fill(0); + for (let i = 0; i < weight.length; i++) { + for (let j = weight[i]; j <= bagSize; j++) { + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); + } + } + console.log(dp); +} +test_CompletePack(); +``` + + + -----------------------