This commit is contained in:
Marege 2026-12-03 17:09:31 +01:00
parent 5fb2cb585f
commit f4a7404f6e

View File

@ -42,6 +42,7 @@ def maximum_value(maximum_weight, items):
def maximum_value_with_items(maximum_weight, items):
# всьо то саме шо в минулому
if items == 0 or items is None or items ==[]:
return (0, [])
if len(items) == 1:
@ -49,4 +50,33 @@ def maximum_value_with_items(maximum_weight, items):
weight = curent_dic.get("weight")
if weight > maximum_weight:
return (0, [])
weight = []
value = []
i = 0
while i < len(items):
curent_dic = items[i]
weight.append(curent_dic.get("weight"))
value.append(curent_dic.get("value"))
i +=1
#batoh_s_vyberom(kapacita, hodnoty, hmotnosti) з prednasky
dp = [0] * (maximum_weight + 1)
vybrate_pri_kapacite = [set() for _ in value]
for i in range(len(value)):
w = weight[i]
v = value[i]
for c in range(maximum_weight, w - 1, -1):
nova_hodnota = v + dp[c - w]
if nova_hodnota > dp[c]:
dp[c] = nova_hodnota
vybrate_pri_kapacite[i].add(c)
vybrane = []
c = maximum_weight
for i in range(len(value) - 1, -1, -1):
if c in vybrate_pri_kapacite[i]:
vybrane.append(items[i])
c -= weight[i]
vybrane.reverse()
return (dp[maximum_weight], vybrane)