Closest Dessert Cost - Problem
The Ice Cream Shop Challenge
You're at an artisanal ice cream shop planning to create the perfect dessert! The shop has
Rules for creating your dessert:
๐ฆ You must choose exactly one ice cream base
๐ You can add zero or more types of toppings
๐ฏ You can use at most two servings of each topping type
Given three inputs:
โข
โข
โข
Goal: Find the total cost that gets you as close as possible to your target budget. If multiple combinations tie, return the lower cost.
You're at an artisanal ice cream shop planning to create the perfect dessert! The shop has
n different ice cream base flavors and m types of delicious toppings to choose from.Rules for creating your dessert:
๐ฆ You must choose exactly one ice cream base
๐ You can add zero or more types of toppings
๐ฏ You can use at most two servings of each topping type
Given three inputs:
โข
baseCosts[] - prices of each ice cream baseโข
toppingCosts[] - prices of each topping typeโข
target - your ideal budgetGoal: Find the total cost that gets you as close as possible to your target budget. If multiple combinations tie, return the lower cost.
Input & Output
example_1.py โ Basic Case
$
Input:
baseCosts = [1,7], toppingCosts = [3,4], target = 10
โบ
Output:
10
๐ก Note:
Consider all combinations: base 1 + toppings = [1, 1+3, 1+3+3, 1+4, 1+4+4, 1+3+4, 1+3+3+4, 1+3+4+4, 1+3+3+4+4] = [1, 4, 7, 5, 9, 8, 11, 12, 15]. Base 7 gives [7, 10, 13, 11, 15, 14, 17, 18, 21]. The closest to target 10 is 10.
example_2.py โ Tie-Breaking
$
Input:
baseCosts = [2,3], toppingCosts = [4,5,100], target = 18
โบ
Output:
17
๐ก Note:
Multiple combinations can achieve difference of 1 from target 18: cost 17 (diff=1) and cost 19 (diff=1). We return 17 as it's the lower cost. One way to get 17: base 3 + two servings of topping 4 + one serving of topping 5 = 3 + 8 + 5 = 16, or base 2 + topping combinations.
example_3.py โ Edge Case
$
Input:
baseCosts = [10], toppingCosts = [1], target = 1
โบ
Output:
10
๐ก Note:
With only one base costing 10 and target 1, the closest we can get is the base itself (10), giving a difference of 9. Adding toppings would only increase the cost further from target.
Constraints
- n == baseCosts.length
- m == toppingCosts.length
- 1 โค n, m โค 10
- 1 โค baseCosts[i], toppingCosts[i] โค 104
- 1 โค target โค 104
Visualization
Tap to expand
Understanding the Visualization
1
Choose Your Base
Start by selecting one ice cream base flavor - this is mandatory
2
Add First Topping
For the first topping type, decide: 0, 1, or 2 servings
3
Continue with Remaining
Repeat the decision for each remaining topping type
4
Calculate and Compare
Sum the total cost and see how close it is to your target budget
Key Takeaway
๐ฏ Key Insight: Use backtracking to systematically explore all possible dessert combinations while pruning branches that cannot lead to better solutions, making the search efficient and optimal.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code