Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e9cecb

Browse files
添加(背包问题理论基础完全背包.md):增加typescript版本
1 parent 28a8d2e commit 1e9cecb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎problems/背包问题理论基础完全背包.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,27 @@ function test_completePack2() {
340340
}
341341
```
342342

343+
TypeScript:
344+
345+
```typescript
346+
// 先遍历物品,再遍历背包容量
347+
function test_CompletePack(): void {
348+
const weight: number[] = [1, 3, 4];
349+
const value: number[] = [15, 20, 30];
350+
const bagSize: number = 4;
351+
const dp: number[] = new Array(bagSize + 1).fill(0);
352+
for (let i = 0; i < weight.length; i++) {
353+
for (let j = weight[i]; j <= bagSize; j++) {
354+
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
355+
}
356+
}
357+
console.log(dp);
358+
}
359+
test_CompletePack();
360+
```
361+
362+
363+
343364

344365
-----------------------
345366
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /