[LeetCode] 1774. Closest Dessert Cost

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.

Example 1:

Input: baseCosts = [1,7], toppingCosts = [3,4], target = 10
Output: 10
Explanation: Consider the following combination (all 0-indexed):
- Choose base 1: cost 7
- Take 1 of topping 0: cost 1 x 3 = 3
- Take 0 of topping 1: cost 0 x 4 = 0
Total: 7 + 3 + 0 = 10.

Example 2:

Input: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18
Output: 17
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.

Example 3:

Input: baseCosts = [3,10], toppingCosts = [2,5], target = 9
Output: 8
Explanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost.

Example 4:

Input: baseCosts = [10], toppingCosts = [1], target = 1
Output: 10
Explanation: Notice that you don't have to have any toppings, but you must have exactly one base.

Constraints:

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

最接近目标价格的甜点成本。

你打算做甜点,现在需要购买配料。目前共有 n 种冰激凌基料和 m 种配料可供选购。而制作甜点需要遵循以下几条规则:

必须选择 一种 冰激凌基料。
可以添加 一种或多种 配料,也可以不添加任何配料。
每种类型的配料 最多两份 。
给你以下三个输入:

baseCosts ,一个长度为 n 的整数数组,其中每个 baseCosts[i] 表示第 i 种冰激凌基料的价格。
toppingCosts,一个长度为 m 的整数数组,其中每个 toppingCosts[i] 表示 一份 第 i 种冰激凌配料的价格。
target ,一个整数,表示你制作甜点的目标价格。
你希望自己做的甜点总成本尽可能接近目标价格 target 。

返回最接近 target 的甜点成本。如果有多种方案,返回 成本相对较低 的一种。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/closest-dessert-cost
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是DFS。这道题是在一定预算范围内找所有的可能性,所以有点类似于backtracking/permutation一类的题。我给出的解法也是类似。如果对permutation一类的题不熟悉可以先做题号比较小的题目。这道题跟一般求permutation类型的题目有点区别的地方就是对于toppings,可以不买,可以买一个,也可以买两个。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     int res = Integer.MAX_VALUE;
 3 
 4     public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
 5         for (int base : baseCosts) {
 6             helper(base, toppingCosts, 0, target);
 7         }
 8         return res;
 9     }
10 
11     private void helper(int curCost, int[] toppingCosts, int index, int target) {
12         // 如果总价比res距离target更近
13         // 或者总价比target更小,则更新
14         if (Math.abs(curCost - target) < Math.abs(res - target)
15                 || Math.abs(curCost - target) == Math.abs(res - target) && curCost < res) {
16             res = curCost;
17         }
18         // base case
19         if (index == toppingCosts.length) {
20             return;
21         }
22         helper(curCost, toppingCosts, index + 1, target); // 不买topping
23         helper(curCost + toppingCosts[index], toppingCosts, index + 1, target); // 买一个topping
24         helper(curCost + 2 * toppingCosts[index], toppingCosts, index + 1, target); // 买两个topping
25     }
26 }

 

LeetCode 题目总结

上一篇:关于情态动词would


下一篇:冻结对象与解冻对象