From 1e9cecb665acd511660a9c8c8af48868e20bb871 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 5 May 2022 16:53:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E8=83=8C=E5=8C=85.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础完全背包.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index faa1dc46..936a80c7 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(); +``` + + + -----------------------