diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index dabdfb2d..bb5e3a03 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -315,6 +315,30 @@ function test () { test(); ``` +### TypeScript + +```typescript +function testWeightBagProblem( + weight: number[], + value: number[], + size: number +): number { + const goodsNum: number = weight.length; + const dp: number[] = new Array(size + 1).fill(0); + for (let i = 0; i < goodsNum; i++) { + for (let j = size; j >= weight[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); + } + } + return dp[size]; +} +const weight = [1, 3, 4]; +const value = [15, 20, 30]; +const size = 4; +console.log(testWeightBagProblem(weight, value, size)); + +``` + -----------------------