mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 08:16:28 +08:00
3f4220de81
* preorder, inorder, postorder -> pre-order, in-order, post-order * Bug fixes * Bug fixes * Update what_is_dsa.md * Sync zh and zh-hant versions * Sync zh and zh-hant versions. * Update performance_evaluation.md and time_complexity.md * Add @khoaxuantu to the landing page. * Sync zh and zh-hant versions * Add @ khoaxuantu to the landing page of zh-hant and en versions.
63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
/**
|
|
* File: unbounded_knapsack.js
|
|
* Created Time: 2023-08-23
|
|
* Author: Gaofer Chou (gaofer-chou@qq.com)
|
|
*/
|
|
|
|
/* 完全背包:動態規劃 */
|
|
function unboundedKnapsackDP(wgt, val, cap) {
|
|
const n = wgt.length;
|
|
// 初始化 dp 表
|
|
const dp = Array.from({ length: n + 1 }, () =>
|
|
Array.from({ length: cap + 1 }, () => 0)
|
|
);
|
|
// 狀態轉移
|
|
for (let i = 1; i <= n; i++) {
|
|
for (let c = 1; c <= cap; c++) {
|
|
if (wgt[i - 1] > c) {
|
|
// 若超過背包容量,則不選物品 i
|
|
dp[i][c] = dp[i - 1][c];
|
|
} else {
|
|
// 不選和選物品 i 這兩種方案的較大值
|
|
dp[i][c] = Math.max(
|
|
dp[i - 1][c],
|
|
dp[i][c - wgt[i - 1]] + val[i - 1]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return dp[n][cap];
|
|
}
|
|
|
|
/* 完全背包:空間最佳化後的動態規劃 */
|
|
function unboundedKnapsackDPComp(wgt, val, cap) {
|
|
const n = wgt.length;
|
|
// 初始化 dp 表
|
|
const dp = Array.from({ length: cap + 1 }, () => 0);
|
|
// 狀態轉移
|
|
for (let i = 1; i <= n; i++) {
|
|
for (let c = 1; c <= cap; c++) {
|
|
if (wgt[i - 1] > c) {
|
|
// 若超過背包容量,則不選物品 i
|
|
dp[c] = dp[c];
|
|
} else {
|
|
// 不選和選物品 i 這兩種方案的較大值
|
|
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
|
}
|
|
}
|
|
}
|
|
return dp[cap];
|
|
}
|
|
|
|
/* Driver Code */
|
|
const wgt = [1, 2, 3];
|
|
const val = [5, 11, 15];
|
|
const cap = 4;
|
|
|
|
// 動態規劃
|
|
let res = unboundedKnapsackDP(wgt, val, cap);
|
|
console.log(`不超過背包容量的最大物品價值為 ${res}`);
|
|
|
|
// 空間最佳化後的動態規劃
|
|
res = unboundedKnapsackDPComp(wgt, val, cap);
|
|
console.log(`不超過背包容量的最大物品價值為 ${res}`);
|