|
| 1 | +1774\. Closest Dessert Cost |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | +* There must be **exactly one** ice cream base. |
| 8 | +* You can add **one or more** types of topping or have no toppings at all. |
| 9 | +* There are **at most two** of **each type** of topping. |
| 10 | + |
| 11 | +You are given three inputs: |
| 12 | + |
| 13 | +* `baseCosts`, an integer array of length `n`, where each `baseCosts[i]` represents the price of the <code>i<sup>th</sup></code> ice cream base flavor. |
| 14 | +* `toppingCosts`, an integer array of length `m`, where each `toppingCosts[i]` is the price of **one** of the <code>i<sup>th</sup></code> topping. |
| 15 | +* `target`, an integer representing your target price for dessert. |
| 16 | + |
| 17 | +You want to make a dessert with a total cost as close to `target` as possible. |
| 18 | + |
| 19 | +Return _the closest possible cost of the dessert to_ `target`. If there are multiple, return _the **lower** one._ |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +**Input:** baseCosts = [1,7], toppingCosts = [3,4], target = 10 |
| 24 | + |
| 25 | +**Output:** 10 |
| 26 | + |
| 27 | +**Explanation:** Consider the following combination (all 0-indexed): |
| 28 | + |
| 29 | +- Choose base 1: cost 7 |
| 30 | + |
| 31 | +- Take 1 of topping 0: cost 1 x 3 = 3 |
| 32 | + |
| 33 | +- Take 0 of topping 1: cost 0 x 4 = 0 |
| 34 | + |
| 35 | +Total: 7 + 3 + 0 = 10. |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +**Input:** baseCosts = [2,3], toppingCosts = [4,5,100], target = 18 |
| 40 | + |
| 41 | +**Output:** 17 |
| 42 | + |
| 43 | +**Explanation:** Consider the following combination (all 0-indexed): - Choose base 1: cost 3 - Take 1 of topping 0: cost 1 x 4 = 4 - Take 2 of topping 1: cost 2 x 5 = 10 - Take 0 of topping 2: cost 0 x 100 = 0 Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18. |
| 44 | + |
| 45 | +**Example 3:** |
| 46 | + |
| 47 | +**Input:** baseCosts = [3,10], toppingCosts = [2,5], target = 9 |
| 48 | + |
| 49 | +**Output:** 8 |
| 50 | + |
| 51 | +**Explanation:** It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost. |
| 52 | + |
| 53 | +**Constraints:** |
| 54 | + |
| 55 | +* `n == baseCosts.length` |
| 56 | +* `m == toppingCosts.length` |
| 57 | +* `1 <= n, m <= 10` |
| 58 | +* <code>1 <= baseCosts[i], toppingCosts[i] <= 10<sup>4</sup></code> |
| 59 | +* <code>1 <= target <= 10<sup>4</sup></code> |
0 commit comments