@@ -38,9 +38,9 @@ def knapsack(
38
38
"""
39
39
40
40
@lru_cache
41
- def knapsack_recur (cap : int , c : int ) -> int :
41
+ def knapsack_recur (capacity : int , counter : int ) -> int :
42
42
# Base Case
43
- if c == 0 or cap == 0 :
43
+ if counter == 0 or capacity == 0 :
44
44
return 0
45
45
46
46
# If weight of the nth item is more than Knapsack of capacity,
@@ -49,17 +49,17 @@ def knapsack_recur(cap: int, c: int) -> int:
49
49
# (1) not included
50
50
# (2) nth item included one or more times (0-N), if allow_repetition is true
51
51
# nth item included only once (0-1), if allow_repetition is false
52
- if weights [c - 1 ] > cap :
53
- return knapsack_recur (cap , c - 1 )
52
+ if weights [counter - 1 ] > capacity :
53
+ return knapsack_recur (capacity , counter - 1 )
54
54
else :
55
- without_new_value = knapsack_recur (cap , c - 1 )
55
+ without_new_value = knapsack_recur (capacity , counter - 1 )
56
56
if allow_repetition :
57
- new_value_included = values [c - 1 ] + knapsack_recur (
58
- cap - weights [c - 1 ], c
57
+ new_value_included = values [counter - 1 ] + knapsack_recur (
58
+ capacity - weights [counter - 1 ], counter
59
59
)
60
60
else :
61
- new_value_included = values [c - 1 ] + knapsack_recur (
62
- cap - weights [c - 1 ], c - 1
61
+ new_value_included = values [counter - 1 ] + knapsack_recur (
62
+ capacity - weights [counter - 1 ], counter - 1
63
63
)
64
64
return max (new_value_included , without_new_value )
65
65
0 commit comments