添加(背包理论基础01背包-2.md):增加typescript版本
This commit is contained in:
parent
6877683c95
commit
36f630b109
|
|
@ -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));
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue