本文主要是介绍力扣 638. 大礼包 python AC,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
dfs、状压
class Solution:def shoppingOffers(self, prices, specials, needs):n = len(needs)@lru_cache(None)def dfs(pre_need):price = sum(prices[i] * pre_need[i] for i in range(n))for special in specials:new_need = tuple(pre_need[i] - special[i] for i in range(n))if min(new_need) >= 0:price = min(price, dfs(new_need) + special[-1])return pricereturn dfs(tuple(needs))
使用tuple来进行状压(因为状态是剩余可购买次数,二进制不够用),用lru_cache加速不可变的tuple
这篇关于力扣 638. 大礼包 python AC的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!