From c7e34a3f9b82d989b408b4303aee17bd192fb91f Mon Sep 17 00:00:00 2001 From: JackZJ <56966563+laerpeeK@users.noreply.github.com> Date: Thu, 28 Apr 2022 08:28:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85JavaScript?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新JavaScript版本二维数组做法,目前文档上的是运行不了的。 --- problems/背包理论基础01背包-1.md | 53 ++++++++++++++---------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index fe940b4c..a844dcf5 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -380,31 +380,40 @@ func main() { ### javascript ```js -function testweightbagproblem (wight, value, size) { - const len = wight.length, - dp = array.from({length: len + 1}).map( - () => array(size + 1).fill(0) - ); - - for(let i = 1; i <= len; i++) { - for(let j = 0; j <= size; j++) { - if(wight[i - 1] <= j) { - dp[i][j] = math.max( - dp[i - 1][j], - value[i - 1] + dp[i - 1][j - wight[i - 1]] - ) - } else { - dp[i][j] = dp[i - 1][j]; - } - } - } +/** + * + * @param {Number []} weight + * @param {Number []} value + * @param {Number} size + * @returns + */ -// console.table(dp); - - return dp[len][size]; +function testWeightBagProblem(weight, value, size) { +const len = weight.length, +dp = Array.from({length: len}).map( +() => Array(size + 1)) //JavaScript 数组是引用类型 +for(let i = 0; i < len; i++) { //初始化最左一列,即背包容量为0时的情况 +dp[i][0] = 0; +} +for(let j = 1; j < size+1; j++) { //初始化第0行, 只有一件物品的情况 +if(weight[0] <= j) { +dp[0][j] = value[0]; +} else { +dp[0][j] = 0; +} +} + +for(let i = 1; i < len; i++) { //dp[i][j]由其左上方元素推导得出 +for(let j = 1; j < size+1; j++) { +if(j < weight[i]) dp[i][j] = dp[i - 1][j]; +else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - weight[i]] + value[i]); +} } -function testWeightBagProblem2 (wight, value, size) { +return dp[len-1][size] //满足条件的最大值 +} + +function testWeightBagProblem2 (wight, value, size)4 { const len = wight.length, dp = Array(size + 1).fill(0); for(let i = 1; i <= len; i++) { From 0694fac15bf6b7d18a53adc9f6ad0c33b51db016 Mon Sep 17 00:00:00 2001 From: JackZJ <56966563+laerpeeK@users.noreply.github.com> Date: Thu, 28 Apr 2022 08:30:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85JavaScript?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新背包理论基础01背包JavaScript代码块 - 二维数组做法1, 目前主线上的是有问题的。 --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index a844dcf5..d6bc5520 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -413,7 +413,7 @@ else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - weight[i]] + value[i]); return dp[len-1][size] //满足条件的最大值 } -function testWeightBagProblem2 (wight, value, size)4 { +function testWeightBagProblem2 (wight, value, size) { const len = wight.length, dp = Array(size + 1).fill(0); for(let i = 1; i <= len; i++) {