Skip to content

Commit 0ff866e

Browse files
Update knapsack.py
1 parent e89dbd9 commit 0ff866e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

knapsack/knapsack.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def knapsack(
3838
"""
3939

4040
@lru_cache
41-
def knapsack_recur(cap: int, c: int) -> int:
41+
def knapsack_recur(capacity: int, counter: int) -> int:
4242
# Base Case
43-
if c == 0 or cap == 0:
43+
if counter == 0 or capacity == 0:
4444
return 0
4545

4646
# If weight of the nth item is more than Knapsack of capacity,
@@ -49,17 +49,17 @@ def knapsack_recur(cap: int, c: int) -> int:
4949
# (1) not included
5050
# (2) nth item included one or more times (0-N), if allow_repetition is true
5151
# 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)
5454
else:
55-
without_new_value = knapsack_recur(cap, c - 1)
55+
without_new_value = knapsack_recur(capacity, counter - 1)
5656
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
5959
)
6060
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
6363
)
6464
return max(new_value_included, without_new_value)
6565

0 commit comments

Comments
 (0)