diff --git a/u11/knapsack.py b/u11/knapsack.py index 5b1ed50..9a35503 100644 --- a/u11/knapsack.py +++ b/u11/knapsack.py @@ -15,7 +15,10 @@ def maximum_value(maximum_weight, items): def maximum_value_with_items(maximum_weight, items): dp = [0] * (maximum_weight + 1) - choose = [] * (maximum_weight + 1) + choose = [] + for pusto in range (maximum_weight + 1): + choose.append([]) + for item in items: item_w = item['weight'] item_v = item['value'] @@ -24,6 +27,6 @@ def maximum_value_with_items(maximum_weight, items): new_v = dp[w - item_w] + item_v if new_v > dp[w]: dp[w] = new_v - choose[w] = choose[w - item_w] + item_v + choose[w] = choose[w - item_w] + [item] return dp[maximum_weight], choose[maximum_weight] \ No newline at end of file