From 3d83e14392b6f4c16ea3ea4bfaf175878dce27cf Mon Sep 17 00:00:00 2001 From: wenmimi Date: Thu, 2 Jun 2022 18:23:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=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=85-1=20GO=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81=20Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-1.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index a40a92ab..e24824b9 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -356,9 +356,13 @@ func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { // 递推公式 for i := 1; i < len(weight); i++ { //正序,也可以倒序 - for j := weight[i];j<= bagweight ; j++ { - dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) - } + for j := 0; j <= bagweight; j++ { + if j < weight[i] { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) + } + } } return dp[len(weight)-1][bagweight] }