File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed
DynamicProgramming/UnboundedKnapsack Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Unbounded Knapsack
2
+
3
+ ## Problem:
4
+
5
+ Given ` weights ` : list of weights where ` weights[i] ` represents the weight of ` i-th ` item,
6
+ ` values ` : list of values where ` values[i] ` represents the value of ` i-th ` item,
7
+ and ` capacity ` , total capacity available,
8
+ Find the maximum values that can be achieved within the limit of total weight.
9
+
10
+ ** same items can be used multiple times**
11
+
12
+ ## Example:
13
+
14
+ ```
15
+ weights = [1,2,3,5]
16
+ values = [1,4,7,10]
17
+ capacity = 8
18
+ Output: 18
19
+ ```
Original file line number Diff line number Diff line change
1
+ def unboundedKnapsack (weights , values , capacity ):
2
+ dp = [[0 for _ in range (capacity + 1 )] for _ in range (len (weights )+ 1 )]
3
+ for i in range (1 , len (dp )):
4
+ for j in range (1 , len (dp [i ])):
5
+ if weights [i - 1 ] <= j :
6
+ dp [i ][j ] = max (dp [i - 1 ][j ], dp [i ][j - weights [i - 1 ]]+ values [i - 1 ])
7
+ else :
8
+ dp [i ][j ] = dp [i - 1 ][j ]
9
+ # print(dp)
10
+ return dp [- 1 ][- 1 ]
11
+
12
+
13
+ weights = [1 , 2 , 3 , 5 ]
14
+ values = [1 , 4 , 7 , 10 ]
15
+ capacity = 8
16
+ print (unboundedKnapsack (weights , values , capacity ))
Original file line number Diff line number Diff line change @@ -68,7 +68,9 @@ Languages used: Java and Python
68
68
- [ Predict Winner] ( DynamicProgramming/PredictWinner )
69
69
- [ Reducing Dishes] ( DynamicProgramming/ReducingDishes )
70
70
- [ Regex Matching] ( DynamicProgramming/RegexMatching )
71
+ - [ Unbounded Knapsack] ( DynamicProgramming/UnboundedKnapsack )
71
72
- [ Unique Paths] ( DynamicProgramming/UniquePaths )
73
+ - [ 0-1 Knapsack] ( DynamicProgramming/ZeroOneKnapsack )
72
74
73
75
### [ Graph] ( ./Graph )
74
76
You can’t perform that action at this time.
0 commit comments