package pep.Day83;
public class Zero_One_Knapsack {
public static void main(String[] args) {
int[] values = {15, 14, 10, 45, 30};
int n = values.length;
int[] weight = {2, 5, 1, 3, 4};
int maxCap = 7;
int[][] dp = new int[n + 1][maxCap + 1];
for (int i = 1; i <= n; i++) {
for (int cap = 1; cap <= maxCap; cap++) {
int val = values[i - 1];
int wt = weight[i - 1];
if (cap >= wt) {
// INCLUDE WALA CASE: jisme weight<= capacity se
int include = val + dp[i - 1][cap - wt];
int exclude = dp[i - 1][cap];
dp[i][cap] = Math.max(include, exclude);
} else {
// EXCLUDE WALA CASE
dp[i][cap] = dp[i - 1][cap];
}
}
}
System.out.println(dp[n][maxCap]);
}
}
Sunday, May 29, 2022
Zero One 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