Uber | Atlassian | Online | Maximise the budget
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-08-13Added By Anonymous
Source: Link
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
•b kind of breads
•s types of sauces
•v types 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:
1.breadPrices, int array of b length where breadPrices[i] is a price of ith bread
2.noVeggies, int representing number of veggies available to choose from
3.saucePrices, int array of length s where each saucePrices[i] is price of ith sauce
4.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:
1n == breadPrices.length
2m == toppingCosts.length
31 <= n, m <= 100
41 <= breadPrices[i], saucePrices[i] <= 10000
51 <= 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.
1 int maxCost = Integer.MAX_VALUE;
2
3 public int findMaxCost(int[] breads, int[] sauces, int budget) {
4 for (int i = 0; i < breads.length; i++) {
5 int cost = breads[i];
6 findMinCostRecursion(cost, 0, sauces, budget);
7 }
8 return maxCost;
9 }
10
11 private void findMinCostRecursion(int cost, int index, int[] sauces, int budget) {
12 if (cost > budget) {
13 return;
14 }
15 if (budget - cost <= maxCost) {
16 maxCost = cost;
17 }
18 for (int i = 0; i < sauces.length; i++) {
19 findMinCostRecursion(cost + sauces[i], i + 1, sauces, budget);
20 findMinCostRecursion(cost + 2 * sauces[i], i + 1, sauces, budget);
21 findMinCostRecursion(cost, i + 1, sauces, budget);
22 }
23 }