u11 update
This commit is contained in:
parent
ef9842c117
commit
3203ab2ca9
@ -2,30 +2,53 @@ def maximum_value(maximum_weight, items):
|
||||
if not items or maximum_weight <= 0:
|
||||
return 0
|
||||
|
||||
sorteditem = sorted(items, key=lambda x: x["value"] / x["weight"], reverse=True)
|
||||
stav = {0, 0}
|
||||
|
||||
for item in items:
|
||||
w = item["weight"]
|
||||
v = item["value"]
|
||||
Novystav = dict(stav)
|
||||
|
||||
TEPvalue = 0
|
||||
currweight = 0
|
||||
for item in sorteditem:
|
||||
if currweight + item["weight"] <= maximum_weight:
|
||||
currweight += item["weight"]
|
||||
TEPvalue += item["value"]
|
||||
return TEPvalue
|
||||
for currW, currV in stav.items():
|
||||
NEwW = currW + W
|
||||
if NEwW <= maximum_weight:
|
||||
NewV = currV + v
|
||||
if NewV > Novystav.get(NEwW, 0):
|
||||
Novystav[NEwW] = NewV
|
||||
|
||||
stav = Novystav
|
||||
|
||||
return max(stav.values())
|
||||
|
||||
def maximum_value_with_items(maximum_weight, items):
|
||||
if not items or maximum_weight <= 0:
|
||||
return 0, []
|
||||
|
||||
sorteditem = sorted(items, key=lambda x: x["value"] / x["weight"], reverse=True)
|
||||
stav = {0: (0,[])}
|
||||
|
||||
TEPvalue = 0
|
||||
currweight = 0
|
||||
vibir = []
|
||||
for item in sorteditem:
|
||||
if currweight + item["weight"] <= maximum_weight:
|
||||
currweight += item["weight"]
|
||||
TEPvalue += item["value"]
|
||||
vibir.append(item)
|
||||
for item in items:
|
||||
w = item["weight"]
|
||||
v = item["value"]
|
||||
Novystav = dict(stav)
|
||||
|
||||
return TEPvalue, vibir
|
||||
|
||||
for currW, currV in stav.items():
|
||||
NEwW = currW + W
|
||||
if NEwW <= maximum_weight:
|
||||
NewV = currV + v
|
||||
if NewV > Novystav.get(NEwW, (0, []))[0]:
|
||||
Novystav[NEwW] = (NewV, vibir + [item])
|
||||
|
||||
stav = Novystav
|
||||
|
||||
BV = 0
|
||||
Bvibir = []
|
||||
for v, vibir in stav.values():
|
||||
if v >BV:
|
||||
BV = v
|
||||
Bvibir = vibir
|
||||
|
||||
return BV, Bvibir
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user