ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1774. Closest Dessert Cost

2021-03-07 06:01:06  阅读:265  来源: 互联网

标签:1774 topping target 10 baseCosts cost toppingCosts LeetCode 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 题目总结

标签:1774,topping,target,10,baseCosts,cost,toppingCosts,LeetCode,Cost
来源: https://www.cnblogs.com/cnoodle/p/14493375.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有