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 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 budget

Goal: 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
๐Ÿฆ Ice Cream Base๐Ÿ“ 0ร—๐Ÿ“ 1ร—๐Ÿ“ 2ร—๐Ÿซ Choc0, 1, 2ร—๐Ÿซ Choc0, 1, 2ร—๐Ÿซ Choc0, 1, 2ร—๐Ÿซ Choc0, 1, 2ร—๐Ÿซ Choc0, 1, 2ร—๐Ÿซ Choc0, 1, 2ร—$8Total$11Total$14Total$17Best!๐ŸŽฏ Target: $15Evaluating costs:โ€ข $8 โ†’ diff = 7โ€ข $11 โ†’ diff = 4โ€ข $14 โ†’ diff = 1โ€ข $17 โ†’ diff = 2Winner: $14 โœ“
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.
Asked in
Google 15 Amazon 8 Microsoft 5 Apple 3
26.0K Views
Medium Frequency
~18 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen