mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1284 from xiaofei-2020/dp18
添加(背包问题理论基础完全背包.md):增加typescript版本
This commit is contained in:
@ -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();
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user