Closest Dessert Cost - Problem

You would like to make dessert and are preparing to buy the ingredients. You have n ice cream base flavors and m types of toppings to choose from. You must follow these rules when making your dessert:

  • There must be exactly one ice cream base.
  • You can add one or more types of topping or have no toppings at all.
  • There are at most two of each type of topping.

You are given three inputs:

  • baseCosts, an integer array of length n, where each baseCosts[i] represents the price of the ith ice cream base flavor.
  • toppingCosts, an integer array of length m, where each toppingCosts[i] is the price of one of the ith topping.
  • target, an integer representing your target price for dessert.

You want to make a dessert with a total cost as close to target as possible.

Return the closest possible cost of the dessert to target. If there are multiple, return the lower one.

Input & Output

Example 1 — Basic Case
$ Input: baseCosts = [1,7], toppingCosts = [3,4], target = 10
Output: 10
💡 Note: Consider base cost 7 with 1 topping of cost 3: 7 + 3 = 10, which exactly equals target 10
Example 2 — Multiple Closest
$ Input: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18
Output: 17
💡 Note: Base 3 + 2×4 + 2×5 = 3 + 8 + 10 = 21 (distance 3), Base 2 + 2×4 + 1×5 = 2 + 8 + 5 = 15 (distance 3), and Base 3 + 2×4 + 1×5 = 17 (distance 1). The closest is 17.
Example 3 — Lower Cost Tie
$ Input: baseCosts = [3,10], toppingCosts = [2,5], target = 9
Output: 8
💡 Note: Base 3 + 1×5 = 8 (distance 1), Base 10 + 0 toppings = 10 (distance 1). Both have same distance but 8 < 10, so return 8

Constraints

  • n == baseCosts.length
  • m == toppingCosts.length
  • 1 ≤ n, m ≤ 10
  • 1 ≤ baseCosts[i], toppingCosts[i] ≤ 104
  • 1 ≤ target ≤ 104

Visualization

Tap to expand
Closest Dessert Cost - Optimal Solution INPUT Ice Cream Bases $1 $7 Toppings (0, 1, or 2 each) $3 x2 $4 x2 baseCosts = [1, 7] toppingCosts = [3, 4] target = 10 Target: $10 ALGORITHM STEPS 1 Choose Base Pick exactly one base 2 Try All Toppings 0, 1, or 2 of each type 3 Calculate Cost Sum base + toppings 4 Track Closest Update if closer to target Example: Base $7 7 + 0 + 0 = 7 7 + 3 + 0 = 10 OK 7 + 0 + 4 = 11 7 + 3 + 4 = 14 7 + 6 + 0 = 13 7 + 0 + 8 = 15 ... FINAL RESULT Winning Combination $7 $3 Cost Breakdown Base: $7 Topping: $3 x 1 Total: $10 Output 10 Exact match found! |10 - 10| = 0 Key Insight: Use DFS/backtracking to explore all combinations. For each base, try adding 0, 1, or 2 of each topping. Track the closest cost to target. If tie exists, prefer the lower cost. Time: O(n * 3^m) where m = toppings. Early termination: if exact match found, return immediately. Prune branches exceeding 2x target. TutorialsPoint - Closest Dessert Cost | Optimal DFS/Backtracking Approach
Asked in
Amazon 15 Google 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 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