From 36f630b109a93685bb241884e5584b327028d5f5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 3 May 2022 12:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-2.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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)); + +``` + -----------------------