Can anyone help me with the question mentioned below :
You are buying a subway sandwich and optimising between possible choices.
You need to choose between following
bkind of breadsstypes of saucesvtypes of veggies
You need to follow these rules while customising your sandwich
- You need exactly 1 bread
- You can add 1 or more types of sauces or no sauces at all
- You can request same sauce for maximum 2 times
- All veggies are free
You will be getting three inputs:
- breadPrices, int array of
blength where breadPrices[i] is a price of ith bread - noVeggies, int representing number of veggies available to choose from
- saucePrices, int array of length
swhere each saucePrices[i] is price of ith sauce - budget, int representing your max budget for the sandwich
You want to customise a sandwich closest to your budget, if there are more than 1 option then choose the one lower than the budget
Constraints:
n == breadPrices.length
m == toppingCosts.length
1 <= n, m <= 100
1 <= breadPrices[i], saucePrices[i] <= 10000
1 <= budget <= 10000Example 1:
Input: breadPrices = [1,7], noVeggies = 5, saucePrices = [3,4], budget = 10
Output: 10
Explanation: Consider the following combination (all 0-indexed):
- Choose bread 1: cost 7
- Take 1 of sauce 0: cost 1 x 3 = 3
- Take 0 of sauce 1: cost 0 x 4 = 0
Total: 7 + 3 + 0 = 10.
I could come up with only the recursive solution, which I fear is not correct even.
int maxCost = Integer.MAX_VALUE;
public int findMaxCost(int[] breads, int[] sauces, int budget) {
for (int i = 0; i < breads.length; i++) {
int cost = breads[i];
findMinCostRecursion(cost, 0, sauces, budget);
}
return maxCost;
}
private void findMinCostRecursion(int cost, int index, int[] sauces, int budget) {
if (cost > budget) {
return;
}
if (budget - cost <= maxCost) {
maxCost = cost;
}
for (int i = 0; i < sauces.length; i++) {
findMinCostRecursion(cost + sauces[i], i + 1, sauces, budget);
findMinCostRecursion(cost + 2 * sauces[i], i + 1, sauces, budget);
findMinCostRecursion(cost, i + 1, sauces, budget);
}
}Comments (1)

