博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 638: Shopping Offers
阅读量:7081 次
发布时间:2019-06-28

本文共 1233 字,大约阅读时间需要 4 分钟。

Note: Standard combination generation:

1. Do not return when find diff < 0. Because i + 1 could satisfy.

2. Since it does not return, it requires check whether it reaches end of needs.

class Solution {    public int shoppingOffers(List
price, List
> special, List
needs) { return generateComb(price, special, needs, new HashMap<>()); } private int generateComb(List
price, List
> special, List
needs, Map
, Integer> map) { if (map.containsKey(needs)) return map.get(needs); int currentValue = getValue(price, needs); for (List
s : special) { List
clone = new ArrayList<>(needs); int i = 0; for (; i < clone.size(); i++) { int diff = clone.get(i) - s.get(i); if (diff < 0) break; clone.set(i, diff); } if (i == needs.size()) currentValue = Math.min(currentValue, s.get(s.size() - 1) + generateComb(price, special, clone, map)); } map.put(needs, currentValue); return currentValue; } private int getValue(List
price, List
needs) { int result = 0; for (int i = 0; i < price.size(); i++) { result += price.get(i) * needs.get(i); } return result; }}

 

转载于:https://www.cnblogs.com/shuashuashua/p/7646383.html

你可能感兴趣的文章
asp.net 获取网站根目录
查看>>
小程序animation动画效果综合应用案例(交流QQ群:604788754)
查看>>
网站头信息的区别所在:
查看>>
web.py实现jsonp
查看>>
zabbix server3.4 使用mailx配置邮件报警
查看>>
nodejs cluster 学习记录
查看>>
基层管理之四:创业之路的一点思考
查看>>
python assert的作用
查看>>
【转】8.caffe:make_mean.sh( 数据平均化 )
查看>>
Location of several networks in brain
查看>>
constexpr函数"QAlgorithmsPrivate::qt_builtin_popcount"不会生成常数表达式
查看>>
C++中struct 和 class的区别
查看>>
第二个冲刺周期day5
查看>>
JS动态绑定数据 一行多列算法
查看>>
浅析Linux服务器集群系统技术
查看>>
python操作redis用法详解
查看>>
hdu 1394 Minimum Inversion Number(线段树)
查看>>
scala类型系统 type关键字
查看>>
Python3_Open()
查看>>
Fiddler使用手册(四)------X5S自动化监测XSS
查看>>