You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# The function returns the maximum value that can be put in a knapsack of a given capacity
5
+
6
+
defknapSack(weight,wt,val,n)
7
+
8
+
rows,cols=n+1,weight+1
9
+
# Create a 2D array to store results of subproblems
10
+
dp=Array.new(rows){Array.new(cols)}
11
+
12
+
foriin(0..n + 1-1)
13
+
forwin(0..weight + 1-1)
14
+
# if the weight is 0 or value is zero, the corresponding cell in the 2D array is set to 0
15
+
ifi == 0 || w == 0
16
+
dp[i][w]=0
17
+
18
+
#If the weight of an element is less than the capacity of the bag, the maximum value of the two cases is taken(Either the element is taken into consideration
0 commit comments