Merge pull request #1502 from wzqwtt/dp08

添加 (背包问题理论基础完全背包、0518.零钱兑换II) Scala版本
This commit is contained in:
程序员Carl
2022-08-01 10:54:50 +08:00
committed by GitHub
2 changed files with 35 additions and 0 deletions

View File

@ -289,7 +289,22 @@ function change(amount: number, coins: number[]): number {
}; };
``` ```
Scala:
```scala
object Solution {
def change(amount: Int, coins: Array[Int]): Int = {
var dp = new Array[Int](amount + 1)
dp(0) = 1
for (i <- 0 until coins.length) {
for (j <- coins(i) to amount) {
dp(j) += dp(j - coins(i))
}
}
dp(amount)
}
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -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)
}
}
```
----------------------- -----------------------