forked from mrpandey1/Leetcode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01 knapsack.py
85 lines (53 loc) · 2 KB
/
01 knapsack.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Solution 1 - Recursive
def knapsack(profits, weights, capacity, n):
# base case
if n == 0 or capacity == 0:
return 0
if weights[n-1] <= capacity:
return max(
profits[n-1] + knapsack(profits, weights,
capacity-weights[n-1], n-1),
knapsack(profits, weights, capacity, n-1)
)
else:
knapsack(profits, weights, capacity, n-1)
print(knapsack([1, 6, 10, ], [1, 2, 3], 5, 3))
# Solution 2 - DP
def solve_knapsack(profits, weights, capacity):
n = len(profits)
dp = [[-1 for x in range(0, capacity+1)] for y in range(0, n)]
return knapsack_recursive_dp(profits, weights, capacity, n, dp)
def knapsack_recursive_dp(profits, weights, capacity, n, dp):
if n == 0 or capacity == 0:
return 0
if dp[n-1][capacity] != -1:
return dp[n-1][capacity]
if weights[n-1] <= capacity:
dp[n-1][capacity] = max(
profits[n-1] +
knapsack_recursive_dp(
profits, weights, capacity-weights[n-1], n-1, dp),
knapsack_recursive_dp(profits, weights, capacity, n-1, dp)
)
return dp[n-1][capacity]
else:
dp[n-1][capacity] = knapsack_recursive_dp(
profits, weights, capacity, n-1, dp)
return dp[n-1][capacity]
print(solve_knapsack([1, 6, 10], [1, 2, 3], 5))
# Solution 3 - DP Bottom up approach
def knapsack_bottom_up(profits, weights, capacity):
# create matrix
dp = [[0 if x == 0 or y == 0 else -
1 for x in range(0, capacity+1)] for y in range(0, len(profits)+1)]
for i in range(1, len(profits)+1):
for j in range(1, capacity+1):
if weights[i-1] <= j:
dp[i][j] = max(
profits[i-1] + dp[i-1][j-weights[i-1]],
dp[i-1][j]
)
else:
dp[i][j] = dp[i-1][j]
return dp[len(profits)][capacity]
print(knapsack_bottom_up([20, 30, 10, 50], [1, 3, 4, 6], 10))