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:...
-
Why appending to a string in Java can become O(n²). 🔴 Problem: Strings are immutable in Java When you do: String s = "" ; s ...
-
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ Given the sorted rotated array nums of unique elements, retu...
-
https://leetcode.com/problems/koko-eating-bananas/description/ Piles: 📦 📦 📦 📦 → 4 piles Hours: ⏰ ⏰ ⏰ ⏰ ⏰ ⏰ → 6 h...



No comments:
Post a Comment