当前位置:网站首页>LeetCode 1774. 最接近目标价格的甜点成本 每日一题
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
你打算做甜点,现在需要购买配料。目前共有 n 种冰激凌基料和 m 种配料可供选购。而制作甜点需要遵循以下几条规则:
必须选择 一种 冰激凌基料。
可以添加 一种或多种 配料,也可以不添加任何配料。
每种类型的配料 最多两份 。
给你以下三个输入:baseCosts ,一个长度为 n 的整数数组,其中每个 baseCosts[i] 表示第 i 种冰激凌基料的价格。
toppingCosts,一个长度为 m 的整数数组,其中每个 toppingCosts[i] 表示 一份 第 i 种冰激凌配料的价格。
target ,一个整数,表示你制作甜点的目标价格。
你希望自己做的甜点总成本尽可能接近目标价格 target 。返回最接近 target 的甜点成本。如果有多种方案,返回 成本相对较低 的一种。
示例 1:
输入:baseCosts = [1,7], toppingCosts = [3,4], target = 10
输出:10
解释:考虑下面的方案组合(所有下标均从 0 开始):
- 选择 1 号基料:成本 7
- 选择 1 份 0 号配料:成本 1 x 3 = 3
- 选择 0 份 1 号配料:成本 0 x 4 = 0
总成本:7 + 3 + 0 = 10 。
示例 2:输入:baseCosts = [2,3], toppingCosts = [4,5,100], target = 18
输出:17
解释:考虑下面的方案组合(所有下标均从 0 开始):
- 选择 1 号基料:成本 3
- 选择 1 份 0 号配料:成本 1 x 4 = 4
- 选择 2 份 1 号配料:成本 2 x 5 = 10
- 选择 0 份 2 号配料:成本 0 x 100 = 0
总成本:3 + 4 + 10 + 0 = 17 。不存在总成本为 18 的甜点制作方案。
示例 3:输入:baseCosts = [3,10], toppingCosts = [2,5], target = 9
输出:8
解释:可以制作总成本为 8 和 10 的甜点。返回 8 ,因为这是成本更低的方案。
示例 4:输入:baseCosts = [10], toppingCosts = [1], target = 1
输出:10
解释:注意,你可以选择不添加任何配料,但你必须选择一种基料。
提示:
n == baseCosts.length
m == toppingCosts.length
1 <= n, m <= 10
1 <= baseCosts[i], toppingCosts[i] <= 104
1 <= target <= 104来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/closest-dessert-cost
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
int ans = Integer.MAX_VALUE;
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
int n = baseCosts.length;
for(int i = 0;i < n;i++){
dfs(toppingCosts,0,baseCosts[i],target);
}
return ans;
}
public void dfs(int[] toppingCosts,int index,int sum,int target){
if(Math.abs(sum - target) < Math.abs(ans - target)) ans = sum;
else if(Math.abs(sum - target) == Math.abs(ans - target)) ans = ans < target ? ans : sum;
//更新结果后在判断
if(index == toppingCosts.length || sum >= target) return;
//加配料
for(int i = 0;i < 3;i++){
dfs(toppingCosts,index + 1,sum + toppingCosts[index] * i,target);
}
}
}边栏推荐
- Three. JS series (1): API structure diagram-1
- 作为Android开发程序员,android高级面试
- Usage of config in laravel
- 值得一看,面试考点与面试技巧
- 最新阿里P7技术体系,妈妈再也不用担心我找工作了
- Binary search tree (basic operation)
- [medical segmentation] attention Unet
- laravel怎么获取到public路径
- Set the route and optimize the URL in thinkphp3.2.3
- 【DesignMode】代理模式(proxy pattern)
猜你喜欢
最新Android高级面试题汇总,Android面试题及答案

Introduction and use of gateway

Balanced binary tree (AVL)

Three. JS series (2): API structure diagram-2

pycharm 终端部启用虚拟环境

Imitate the choice of enterprise wechat conference room

Talk about the realization of authority control and transaction record function of SAP system

掌握这套精编Android高级面试题解析,oppoAndroid面试题

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑

字节跳动高工面试,轻松入门flutter
随机推荐
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
模块六
23. 合并K个升序链表-c语言
编程模式-表驱动编程
three. JS create cool snow effect
二叉搜索树(特性篇)
[designmode] flyweight pattern
运算符
Cesium(3):ThirdParty/zip. js
Detailed explanation of several ideas for implementing timed tasks in PHP
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
AutoLISP series (3): function function 3
ByteDance Android gold, silver and four analysis, Android interview question app
Find tags in prefab in unity editing mode
Common training data set formats for target tracking
射线与OBB相交检测
laravel中将session由文件保存改为数据库保存
数据中台落地实施之法
Lie cow count (spring daily question 53)
Interface oriented programming