Merge pull request #1278 from xiaofei-2020/dp12

添加(背包理论基础01背包-2.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-05-22 12:51:30 +08:00
committed by GitHub

View File

@ -315,6 +315,30 @@ function test () {
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));
```
----------------------- -----------------------