|
| 1 | +# 1774. Closest Dessert Cost |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Dynamic Programming, Backtracking. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- There must be **exactly one** ice cream base. |
| 14 | + |
| 15 | +- You can add **one or more** types of topping or have no toppings at all. |
| 16 | + |
| 17 | +- There are **at most two** of **each type** of topping. |
| 18 | + |
| 19 | + |
| 20 | +You are given three inputs: |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +- `baseCosts`, an integer array of length `n`, where each `baseCosts[i]` represents the price of the `ith` ice cream base flavor. |
| 25 | + |
| 26 | +- `toppingCosts`, an integer array of length `m`, where each `toppingCosts[i]` is the price of **one** of the `ith` topping. |
| 27 | + |
| 28 | +- `target`, an integer representing your target price for dessert. |
| 29 | + |
| 30 | + |
| 31 | +You want to make a dessert with a total cost as close to `target` as possible. |
| 32 | + |
| 33 | +Return **the closest possible cost of the dessert to **`target`. If there are multiple, return **the **lower** one.** |
| 34 | + |
| 35 | + |
| 36 | +Example 1: |
| 37 | + |
| 38 | +``` |
| 39 | +Input: baseCosts = [1,7], toppingCosts = [3,4], target = 10 |
| 40 | +Output: 10 |
| 41 | +Explanation: Consider the following combination (all 0-indexed): |
| 42 | +- Choose base 1: cost 7 |
| 43 | +- Take 1 of topping 0: cost 1 x 3 = 3 |
| 44 | +- Take 0 of topping 1: cost 0 x 4 = 0 |
| 45 | +Total: 7 + 3 + 0 = 10. |
| 46 | +``` |
| 47 | + |
| 48 | +Example 2: |
| 49 | + |
| 50 | +``` |
| 51 | +Input: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18 |
| 52 | +Output: 17 |
| 53 | +Explanation: Consider the following combination (all 0-indexed): |
| 54 | +- Choose base 1: cost 3 |
| 55 | +- Take 1 of topping 0: cost 1 x 4 = 4 |
| 56 | +- Take 2 of topping 1: cost 2 x 5 = 10 |
| 57 | +- Take 0 of topping 2: cost 0 x 100 = 0 |
| 58 | +Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18. |
| 59 | +``` |
| 60 | + |
| 61 | +Example 3: |
| 62 | + |
| 63 | +``` |
| 64 | +Input: baseCosts = [3,10], toppingCosts = [2,5], target = 9 |
| 65 | +Output: 8 |
| 66 | +Explanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost. |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +**Constraints:** |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +- `n == baseCosts.length` |
| 75 | + |
| 76 | +- `m == toppingCosts.length` |
| 77 | + |
| 78 | +- `1 <= n, m <= 10` |
| 79 | + |
| 80 | +- `1 <= baseCosts[i], toppingCosts[i] <= 104` |
| 81 | + |
| 82 | +- `1 <= target <= 104` |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +## Solution |
| 87 | + |
| 88 | +```javascript |
| 89 | +/** |
| 90 | + * @param {number[]} baseCosts |
| 91 | + * @param {number[]} toppingCosts |
| 92 | + * @param {number} target |
| 93 | + * @return {number} |
| 94 | + */ |
| 95 | +var closestCost = function(baseCosts, toppingCosts, target) { |
| 96 | + var res = Number.MAX_SAFE_INTEGER; |
| 97 | + for (var i = 0; i < baseCosts.length; i++) { |
| 98 | + res = closest(target, res, baseCosts[i] + helper(toppingCosts, target - baseCosts[i], 0)); |
| 99 | + } |
| 100 | + return res; |
| 101 | +}; |
| 102 | + |
| 103 | +var helper = function(toppingCosts, target, i) { |
| 104 | + if (i === toppingCosts.length) return 0; |
| 105 | + if (target <= 0) return 0; |
| 106 | + var res = Number.MAX_SAFE_INTEGER; |
| 107 | + res = closest(target, res, helper(toppingCosts, target, i + 1)); |
| 108 | + res = closest(target, res, toppingCosts[i] + helper(toppingCosts, target - toppingCosts[i], i + 1)); |
| 109 | + res = closest(target, res, toppingCosts[i] * 2 + helper(toppingCosts, target - toppingCosts[i] * 2, i + 1)); |
| 110 | + return res; |
| 111 | +}; |
| 112 | + |
| 113 | +var closest = function(target, num1, num2) { |
| 114 | + var diff1 = Math.abs(num1 - target); |
| 115 | + var diff2 = Math.abs(num2 - target); |
| 116 | + if (diff1 === diff2) return Math.min(num1, num2); |
| 117 | + return diff1 < diff2 ? num1 : num2; |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +**Explain:** |
| 122 | + |
| 123 | +nope. |
| 124 | + |
| 125 | +**Complexity:** |
| 126 | + |
| 127 | +* Time complexity : O(n * m ^ 3). |
| 128 | +* Space complexity : O(m). |
0 commit comments