638. Gift package

In the LeetCode store, there are many items for sale.

However, there are also some gift packages, and each gift package bundles a set of items at a favorable price.

Given the price of each item, each gift package contains a list of items and a list of items to be purchased. Please output the exact minimum cost to complete the pending list.

Each gift package is described by a set of data in an array, the last number represents the price of the gift package, and the other numbers indicate the number of other types of items contained.

Any gift pack can be purchased unlimited times.

Example 1:

Input: [2,5], [[3,0,5], [1,2,10]], [3,2]
Output: 14
Explanation:
There are two items A and B, the price is ¥ 2 and ¥ 5.
Spree 1, you can buy 3A and 0B for ¥ 5.
Spree 2, you can buy 1A and 2B for ¥ 10.
You need to buy 3 A and 2 B, so you paid ¥ 10 to buy 1A and 2B (Gift Pack 2), and ¥ 4 to buy 2A.
Example 2:

Input: [2,3,4], [[1,1,0,4], [2,2,1,9]], [1,2,1]
Output: 11
Explanation:
A, B, C The prices are ¥ 2, ¥ 3, ¥ 4.
You can buy 1A and 1B with ¥ 4, or you can buy 2A, 2B and 1C with ¥ 9.
You need to buy 1A, 2B, and 1C, so you paid ¥ 4 for 1A and 1B (Gift Pack 1), and ¥ 3 for 1B, and ¥ 4 for 1C.
You can't buy items that are beyond the to-be-purchased list, although it is cheaper to buy the spree 2.

Note:
Up to 6 items and 100 gift packages.
For each item, you only need to purchase a maximum of 6.
You cannot buy items that are beyond the list to be purchased, even if they are cheaper.

Source: LeetCode


What does this question say, although it has been written for about an hour, it is really a question of independent thinking and completion in the middle school.
Quickly write about the formation of
this question : 1. This question should be recursive. How to solve recursion?
2. Austria, you need to list all possible solutions recursively, and find the smallest answer.
3. How to write several elements of recursion?

  • End condition: Can't buy more, just need to be right. Later, it developed to have no surplus (all you want to buy).
    There is a process in the middle, thinking about this recursive problem to be solved : just buy everything . Thinking of here is a sudden realization, so the end condition must be that the things are bought out. At first I thought about the end condition of recursion, but I didn't think of the actual problem solved by this recursion. Deviated from reality. Think about it next time, to what extent I did it before I did n’t do it .
  • Subject: The theme must be shopping
    • First buy a spree, and then buy a single product.
    • Buy a spree to see if you can buy it. Can buy, recursive; can not buy, see the next big gift package.
    • Finally, check for missing vacancies, and buy the unsold items.
    • Hesitated after writing, it should be the 优先级 (当时理解成代码的执行顺序 )same as the gift package, and should be put together. Then I thought about it again, the gift package must be discounted.

4. Finally found that the answer is wrong, think about it turned out that the middle variable is wrong (ans- = special [i] [j]; there is no such line). Changed and succeeded.
Finally, regarding the priority problem, add ans- = special [i] [j], and then think about it, so the priority is the same. The understanding now is that all have the opportunity to buy it.
Or just change it dfs(price,special,needs,ans+special[i][j]).

int eans=0x3f3f3f3f;
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
      
        dfs(price,special,needs,0);
        return eans;
    }
    void dfs(vector<int>& price, vector<vector<int>>& special, vector<int>& needs,int ans){
        int flag=0;
        for(auto a:needs){
            flag+=a;
        }
        if(flag==0){
            eans=min(ans,eans);
            return;
        } 
        //需要的  都  买完了。所以结束递归,返回价钱。

        //开始买大礼包
        for(int i=0;i<special.size();i++){
            //看看大礼包能不能买,不能买的的话,看下一个大礼包
            int flag1=true;
            int j;
            for(j=0;j<needs.size();j++){
                if(special[i][j]>needs[j]) {
                    flag1=false;
                    break;
                }
            }

            //要是可以买大礼包,则递归
            if(flag1){
                ans+=special[i][j];//把大礼包的价钱加上
                for(j=0;j<special[i].size()-1;j++) needs[j]-=special[i][j];//把需求减去
                dfs(price,special,needs,ans);//继续看下一个礼包,
                //递归之后,需要需求加回来
                for(j=0;j<special[i].size()-1;j++) needs[j]+=special[i][j];
                ans-=special[i][j];//把大礼包的价钱减去
            }
        }
        //大礼包买完了,开始买单价商品
        vector<int>tep=needs;
        int tepp=0;
        for(int i=0;i<needs.size();i++){
            if(needs[i]!=0){
                tepp+=needs[i]*price[i];
                needs[i]=0;
            }
        }
        ans+=tepp;
        //cout<<ans<<endl;
        dfs(price,special,needs,ans);
        needs=tep;
        ans-=tepp;
    }
Published 161 original articles · Like 68 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/qq_43179428/article/details/104310922