mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加 背包问题理论基础完全背包.md Scala版本
This commit is contained in:
@ -359,7 +359,27 @@ function test_CompletePack(): void {
|
|||||||
test_CompletePack();
|
test_CompletePack();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Scala:
|
||||||
|
|
||||||
|
```scala
|
||||||
|
// 先遍历物品,再遍历背包容量
|
||||||
|
object Solution {
|
||||||
|
def test_CompletePack() {
|
||||||
|
var weight = Array[Int](1, 3, 4)
|
||||||
|
var value = Array[Int](15, 20, 30)
|
||||||
|
var baseweight = 4
|
||||||
|
|
||||||
|
var dp = new Array[Int](baseweight + 1)
|
||||||
|
|
||||||
|
for (i <- 0 until weight.length) {
|
||||||
|
for (j <- weight(i) to baseweight) {
|
||||||
|
dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp(baseweight)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user