package pep.Day84;
public class Unbounded_Knapsack {
public static void main(String[] args) {
int[] values = {15, 14, 10, 45, 30};
int[] weight = {2, 5, 1, 3, 4};
int maxCapacity = 7;
int[] dp = new int[maxCapacity + 1];
for (int cap = 0; cap <= maxCapacity; cap++) {
int max = 0;
for (int i = 0; i < values.length; i++) {
int val = values[i];
int wt = weight[i];
// check item
if (cap >= wt) {
int amt = val + dp[cap - wt];
max = Math.max(max, amt);
}
}
dp[cap] = max;
}
System.out.println(dp[maxCapacity]);
}
}
Sunday, May 29, 2022
Unbounded Knapsack
Subscribe to:
Post Comments (Atom)
Diagonal Traversal
eg. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 6 11 16 2 7 12 3 8 4 Approach:...
-
https://leetcode.com/problems/reverse-integer/description/ Integer.MAX_VALUE = 2,147,483,647 = 2 31 - 1 Integer.MIN_VALUE = -2,147,483,64...
-
https://leetcode.com/problems/two-sum/description/ Given the constraints: 2 <= nums.length <= 10⁴ -10⁹ <= nums[i] <= 10⁹ -10...
-
For this particular question, we only have the In - region. So, the code looks like : package pep.Day7 ; public class TowerOfHanoi { p...



No comments:
Post a Comment