Ctrl K

Dessert Near the Budget

Medium
A café assembles a dessert. First you pick exactly one base: base i costs bases[i]. Then you may add toppings: topping j costs toppings[j] per serving, and you can take 0, 1 or 2 servings of each. Skipping toppings altogether is fine too. You want the dessert's price to be as close to budget as possible, above or below. Return that closest achievable price. If two prices are equally close, return the lower one.
Example 1
bases=[5, 9], toppings=[2, 6], budget=15 → 15

The base at 9 plus one serving of the topping at 6 comes to exactly 15.

Example 2
bases=[4], toppings=[3], budget=8 → 7

The possible prices are 4, 7 and 10. The closest to 8 is 7, off by 1.

Example 3
bases=[6, 12], toppings=[7], budget=9 → 6

The prices are 6, 13, 20, 12, 19 and 26. Both 6 and 12 are 3 away from 9, so the cheaper one, 6, wins.

Constraints
  • 1 ≤ len(bases), len(toppings) ≤ 10
  • 1 ≤ bases[i], toppings[j] ≤ 10⁴
  • 1 ≤ budget ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare Frame From Sticks MediumPlacing Plus and Minus Signs MediumSeating by Divisibility MediumCheapest Basket with Bundles MediumGroups With Equal Sums Medium
loading Python…
Input
bases=[5, 9], toppings=[2, 6], budget=15
Expected
15

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.