mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
0e789ce3ec
commit
1d756e4621
@ -22,8 +22,10 @@ Dynamic Programming
|
|||||||
for more information visit https://en.wikipedia.org/wiki/Memoization
|
for more information visit https://en.wikipedia.org/wiki/Memoization
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def knapsack(values:list, weights:list, num_of_items:int, max_weight:int, dp:list
|
|
||||||
) -> int:
|
def knapsack(
|
||||||
|
values: list, weights: list, num_of_items: int, max_weight: int, dp: list
|
||||||
|
) -> int:
|
||||||
"""
|
"""
|
||||||
Function description is as follows-
|
Function description is as follows-
|
||||||
:param weights: Take a list of weights
|
:param weights: Take a list of weights
|
||||||
@ -64,25 +66,30 @@ def knapsack(values:list, weights:list, num_of_items:int, max_weight:int, dp:lis
|
|||||||
>>> knapsack(values,wt,n,w,dp)
|
>>> knapsack(values,wt,n,w,dp)
|
||||||
75
|
75
|
||||||
"""
|
"""
|
||||||
#no profit gain if any of these two become zero
|
# no profit gain if any of these two become zero
|
||||||
if max_weight == 0 or num_of_items == 0:
|
if max_weight == 0 or num_of_items == 0:
|
||||||
dp[num_of_items][max_weight] = 0
|
dp[num_of_items][max_weight] = 0
|
||||||
return 0
|
return 0
|
||||||
#if this case is previously encountered => maximum gain for this case is already
|
# if this case is previously encountered => maximum gain for this case is already
|
||||||
elif dp[num_of_items][max_weight] != -1:
|
elif dp[num_of_items][max_weight] != -1:
|
||||||
#in dp table
|
# in dp table
|
||||||
return dp[num_of_items][max_weight]
|
return dp[num_of_items][max_weight]
|
||||||
|
|
||||||
#if the item can be included in the bag
|
# if the item can be included in the bag
|
||||||
elif weights[num_of_items-1] <= max_weight:
|
elif weights[num_of_items - 1] <= max_weight:
|
||||||
# ans1 stores the maximum profit if the item at
|
# ans1 stores the maximum profit if the item at
|
||||||
# index num_of_items -1 is included in the bag
|
# index num_of_items -1 is included in the bag
|
||||||
incl = knapsack(values,weights,num_of_items-1,
|
incl = knapsack(
|
||||||
max_weight-weights[num_of_items-1],dp)
|
values,
|
||||||
|
weights,
|
||||||
|
num_of_items - 1,
|
||||||
|
max_weight - weights[num_of_items - 1],
|
||||||
|
dp,
|
||||||
|
)
|
||||||
ans1 = values[num_of_items - 1] + incl
|
ans1 = values[num_of_items - 1] + incl
|
||||||
# ans2 stores the maximum profit if the item at
|
# ans2 stores the maximum profit if the item at
|
||||||
# index num_of_items -1 is not included in the bag
|
# index num_of_items -1 is not included in the bag
|
||||||
ans2 = knapsack(values, weights, num_of_items-1, max_weight, dp)
|
ans2 = knapsack(values, weights, num_of_items - 1, max_weight, dp)
|
||||||
# the final answer is the maximum profit gained from any of ans1 or ans2
|
# the final answer is the maximum profit gained from any of ans1 or ans2
|
||||||
dp[num_of_items][max_weight] = max(ans1, ans2)
|
dp[num_of_items][max_weight] = max(ans1, ans2)
|
||||||
return dp[num_of_items][max_weight]
|
return dp[num_of_items][max_weight]
|
||||||
@ -90,12 +97,13 @@ def knapsack(values:list, weights:list, num_of_items:int, max_weight:int, dp:lis
|
|||||||
# if the item's weight exceeds the max_weight of the bag
|
# if the item's weight exceeds the max_weight of the bag
|
||||||
# => it cannot be included in the bag
|
# => it cannot be included in the bag
|
||||||
else:
|
else:
|
||||||
dp[num_of_items][max_weight] = knapsack(values, weights,
|
dp[num_of_items][max_weight] = knapsack(
|
||||||
num_of_items-1, max_weight, dp)
|
values, weights, num_of_items - 1, max_weight, dp
|
||||||
|
)
|
||||||
return dp[num_of_items][max_weight]
|
return dp[num_of_items][max_weight]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod(name='knapsack', verbose=True)
|
|
||||||
|
doctest.testmod(name="knapsack", verbose=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user