-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathknapsack_optimized.py
26 lines (24 loc) · 1.09 KB
/
knapsack_optimized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from typing import List
def knapsack_optimized(cap: int, weights: List[int], values: List[int]) -> int:
n = len(values)
# Initialize 'prev_row' as the DP values of the row below the
# current row.
prev_row = [0] * (cap + 1)
for i in range(n - 1, -1, -1):
# Set the first cell of the 'curr_row' to 0 to set the base
# case for this row. This is done by initializing the entire
# row to 0.
curr_row = [0] * (cap + 1)
for c in range(1, cap + 1):
# If item 'i' fits in the current knapsack capacity, the
# maximum value at 'curr_row[c]' is the largest of either:
# 1. The maximum value if we include item 'i'.
# 2. The maximum value if we exclude item 'i'.
if weights[i] <= c:
curr_row[c] = max(values[i] + prev_row[c - weights[i]], prev_row[c])
# If item 'i' doesn't fit, we exclude it.
else:
curr_row[c] = prev_row[c]
# Set 'prev_row' to 'curr_row' values for the next iteration.
prev_row = curr_row
return prev_row[cap]