From c70316d7aa59caffe1a0cec9b1c818fcc867a411 Mon Sep 17 00:00:00 2001 From: wzqwtt <1722249371@qq.com> Date: Tue, 28 Jun 2022 21:30:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E8=83=8C=E5=8C=85.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础完全背包.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 54e772e0..fc4609a6 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -359,7 +359,27 @@ function test_CompletePack(): void { 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) + } +} +``` ----------------------- From bdd4c83b642dfa5b2c7c5a6b6fd214aa8e0488bb Mon Sep 17 00:00:00 2001 From: wzqwtt <1722249371@qq.com> Date: Tue, 28 Jun 2022 21:56:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200518.=E9=9B=B6?= =?UTF-8?q?=E9=92=B1=E5=85=91=E6=8D=A2II.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0518.零钱兑换II.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 222a10d7..3abb9601 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -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) + } +} +``` -----------------------