Last active
October 30, 2017 23:39
-
-
Save scaffeinate/13018b72238e1b30236f1126ce7beed1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by sudharti on 10/30/17. | |
*/ | |
public class BudgetShopping { | |
static int budgetShopping(int n, int[] bundleQuantities, int[] bundleCosts) { | |
return budgetShopping(n, bundleQuantities, bundleCosts, new HashMap<>()); | |
} | |
static int budgetShopping(int n, int[] bundleQuantities, int[] bundleCosts, Map<Integer, Integer> memo) { | |
if (n <= 0) { | |
return 0; | |
} | |
if (!memo.containsKey(n)) { | |
int max = 0; | |
for (int i = 0; i < bundleQuantities.length; i++) { | |
if (n >= bundleCosts[i]) { | |
max = Math.max(max, bundleQuantities[i] + budgetShopping(n - bundleCosts[i], bundleQuantities, bundleCosts, memo)); | |
} | |
} | |
memo.put(n, max); | |
} | |
return memo.get(n); | |
} | |
public static void main(String[] args) { | |
System.out.println(budgetShopping(50, new int[]{20, 19}, new int[]{24, 20})); | |
System.out.println(budgetShopping(60, new int[]{20, 19}, new int[]{24, 20})); | |
System.out.println(budgetShopping(4, new int[]{10}, new int[]{2})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment