638. Shopping Offers 深度优先遍历、递归、存储技术(基于递归的动态规划)
来源:互联网 发布:淘宝判定为广告的评价 编辑:程序博客网 时间:2024/05/28 22:07
In LeetCode Store, there are some kinds of items to sell. Each item has a price.
However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
You are given the each item's price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers.
Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.
You could use any of special offers as many times as you want.
Example 1:
Input: [2,5], [[3,0,5],[1,2,10]], [3,2]Output: 14Explanation: There are two kinds of items, A and B. Their prices are $2 and $5 respectively. In special offer 1, you can pay $5 for 3A and 0BIn special offer 2, you can pay $10 for 1A and 2B. You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
Example 2:
Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]Output: 11Explanation: The price of A is $2, and $3 for B, $4 for C. You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. You cannot add more items, though only $9 for 2A ,2B and 1C.
Note:
- There are at most 6 kinds of items, 100 special offers.
- For each item, you need to buy at most 6 of them.
- You are not allowed to buy more items than you want, even if that would lower the overall price.
class Solution {public: int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) { return shopping(price,special,needs); } //下面是递归函数 int shopping(vector<int>& price, vector<vector<int>>& special, vector<int>& needs){ //首先求出不大折扣所需的钱是多少 int res=0; for(int i=0;i<price.size();i++) { res+=price[i]*needs[i]; } //下面的循环,每次选择一个折扣方案,看看是否可行(可行指的是采用折扣方案不会买到多余的商品) //如果可行,就将减掉折扣之后的needs传入到下一个递归中去 for(auto s:special) { //针对每一种折扣方案,都首先复制一份购物单列表,因为这是一种尝试 vector<int> clone(needs); int j=0; for(;j<clone.size();j++) { clone[j]=clone[j]-s[j]; if(clone[j]<0)//如果这种折扣方案的某个商品数量大于当前这种商品的需求量,这种折扣方案是不可行的 break; } if(j==clone.size()) res=min(res,s[j]+shopping(price,special,clone)); } return res; }};思路2:
class Solution {public: int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) { map<vector<int>,int> p; return shopping(price,special,needs,p); } //下面是递归函数 int shopping(vector<int>& price, vector<vector<int>>& special, vector<int>& needs,map<vector<int>,int>& p){ //首先求出不大折扣所需的钱是多少 if(p.find(needs)!=p.end())//如果当前的needs最小花费已经球出来了,直接返回 return p[needs]; int res=0; for(int i=0;i<price.size();i++) { res+=price[i]*needs[i]; } //下面的循环,每次选择一个折扣方案,看看是否可行(可行指的是采用折扣方案不会买到多余的商品) //如果可行,就将减掉折扣之后的needs传入到下一个递归中去 for(auto s:special) { //针对每一种折扣方案,都首先复制一份购物单列表,因为这是一种尝试 vector<int> clone(needs); int j=0; for(;j<clone.size();j++) { clone[j]=clone[j]-s[j]; if(clone[j]<0)//如果这种折扣方案的某个商品数量大于当前这种商品的需求量,这种折扣方案是不可行的 break; } if(j==clone.size()) res=min(res,s[j]+shopping(price,special,clone,p)); } p[needs]=res;//记录当前needs的最小花费 return res; }};
- 638. Shopping Offers 深度优先遍历、递归、存储技术(基于递归的动态规划)
- 图的深度优先遍历----递归
- 深度优先遍历算法的非递归
- 树的非递归深度优先遍历
- 深度优先遍历的非递归
- leetcode 638. Shopping Offers 递归求解最佳购物优惠
- 邻接表存储图的深度优先、广度优先遍历非递归算法
- 二叉树的深度优先遍历(递归、非递归),广度优先遍历(递归)
- 非递归深度优先遍历
- php深度优先递归遍历
- [动态规划]3.3.2 Shopping Offers
- 图的遍历,非递归版本,深度优先遍历
- 图的深度优先遍历,基于邻接链表的非递归实现
- 数据结构笔记--图的邻接表存储及非递归深度优先遍历
- 邻接矩阵的深度优先遍历(递归以及非递归),广度优先遍历
- 图的深度优先遍历(递归与非递归算法)和广度优先遍历
- 二叉树深度优先遍历的非递归算法
- 深度优先遍历算法的非递归实现
- 19. Remove Nth Node From End of List
- 浅析JVM类加载机制
- 515. Find Largest Value in Each Tree Row 找到数每一层的最大值
- C++动态分配内存空间 : new 和 delete
- 576. Out of Boundary Paths
- 638. Shopping Offers 深度优先遍历、递归、存储技术(基于递归的动态规划)
- oracle触发器分类
- redis的自启设置
- DFS(深度搜索)——基于回溯法和递归
- Unity学习2 鼠标点击控制物体移动
- C++语法基础之关键字与标识符(10月4日暂存)
- 295. Find Median from Data Stream 剑指offer 数据流中的中位数
- 42. Trapping Rain Water 及类似题目 407. Trapping Rain Water II 11. Container With Most Water
- linux内核--资源管理