Many bug fixes and improvements (#1270)

* Add Ruby and Kotlin icons
Add the avatar of @curtishd

* Update README

* Synchronize zh-hant and zh versions.

* Translate the pythontutor blocks to traditional Chinese

* Fix en/mkdocs.yml

* Update the landing page of the en version.

* Fix the Dockerfile

* Refine the en landingpage

* Fix en landing page

* Reset the README.md
This commit is contained in:
Yudong Jin
2024-04-11 20:18:19 +08:00
committed by GitHub
parent 07977184ad
commit b2f0d4603d
192 changed files with 2382 additions and 1196 deletions

View File

@ -9,11 +9,7 @@ package chapter_dynamic_programming
import kotlin.math.max
/* 完全背包:動態規劃 */
fun unboundedKnapsackDP(
wgt: IntArray,
value: IntArray,
cap: Int
): Int {
fun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
val n = wgt.size
// 初始化 dp 表
val dp = Array(n + 1) { IntArray(cap + 1) }
@ -25,8 +21,7 @@ fun unboundedKnapsackDP(
dp[i][c] = dp[i - 1][c]
} else {
// 不選和選物品 i 這兩種方案的較大值
dp[i][c] = max(dp[i - 1][c].toDouble(), (dp[i][c - wgt[i - 1]] + value[i - 1]).toDouble())
.toInt()
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])
}
}
}
@ -36,7 +31,7 @@ fun unboundedKnapsackDP(
/* 完全背包:空間最佳化後的動態規劃 */
fun unboundedKnapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
@ -50,8 +45,7 @@ fun unboundedKnapsackDPComp(
dp[c] = dp[c]
} else {
// 不選和選物品 i 這兩種方案的較大值
dp[c] =
max(dp[c].toDouble(), (dp[c - wgt[i - 1]] + value[i - 1]).toDouble()).toInt()
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
@ -61,14 +55,14 @@ fun unboundedKnapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(1, 2, 3)
val value = intArrayOf(5, 11, 15)
val _val = intArrayOf(5, 11, 15)
val cap = 4
// 動態規劃
var res = unboundedKnapsackDP(wgt, value, cap)
var res = unboundedKnapsackDP(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res")
// 空間最佳化後的動態規劃
res = unboundedKnapsackDPComp(wgt, value, cap)
res = unboundedKnapsackDPComp(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res")
}