[LeetCode] Shopping Offers 购物优惠

简介:

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: 14
Explanation: 
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 0B
In 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: 11
Explanation: 
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:

  1. There are at most 6 kinds of items, 100 special offers.
  2. For each item, you need to buy at most 6 of them.
  3. You are not allowed to buy more items than you want, even if that would lower the overall price.

这道题说有一些商品,各自有不同的价格,然后给我们了一些优惠券,可以在优惠的价格买各种商品若干个,要求我们每个商品要买特定的个数,问我们使用优惠券能少花多少钱,注意优惠券可以重复使用,而且商品不能多买。那么我们可以先求出不使用任何商品需要花的钱数作为结果res的初始值,然后我们遍历每一个coupon,定义一个变量isValid表示当前coupon可以使用,然后遍历每一个商品,如果某个商品需要的个数小于coupon中提供的个数,说明当前coupon不可用,isValid标记为false。如果遍历完了发现isValid还为true的话,表明该coupon可用,我们可以更新结果res,对剩余的needs调用递归并且加上使用该coupon需要付的钱数。最后别忘了恢复needs的状态,参见代码如下: 

解法一:

public:
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
        int res = 0, n = price.size();
        for (int i = 0; i < n; ++i) {
            res += price[i] * needs[i];
        }
        for (auto offer : special) {
            bool isValid = true;
            for (int j = 0; j < n; ++j) {
                if (needs[j] - offer[j] < 0) isValid = false;
                needs[j] -= offer[j];
            }
            if (isValid) {
                res = min(res, shoppingOffers(price, special, needs) + offer.back());
            }
            for (int j = 0; j < n; ++j) {
                needs[j] += offer[j];
            }
        }
        return res;
    }
};

下面这种解法也是递归的写法,总的来说思路跟上面没有啥差别,应该不难理解,参见代码如下:

解法二:

public:
    int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
        int res = inner_product(price.begin(), price.end(), needs.begin(), 0);
        for (auto offer : special) {
            vector<int> r = helper(offer, needs);
            if (r.empty()) continue;
            res = min(res, shoppingOffers(price, special, r) + offer.back());
        }
        return res;
    }
    vector<int> helper(vector<int>& offer, vector<int>& needs) {
        vector<int> r(needs.size(), 0);
        for (int i = 0; i < needs.size(); ++i) {
            if (offer[i] > needs[i]) return {};
            r[i] = needs[i] - offer[i];
        }
        return r;
    }
}; 

参考资料:

https://discuss.leetcode.com/topic/95201/c-solution

https://discuss.leetcode.com/topic/95200/simple-java-recursive-solution

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Shopping Offers 购物优惠

,如需转载请自行联系原博主。

相关文章
|
8月前
|
SQL 存储 前端开发
商品热销排行【项目 商城】
商品热销排行【项目 商城】
25 0
|
11月前
7-69 超市促销
7-69 超市促销
64 0
|
7月前
|
缓存
美多商城商品部分(三)
美多商城商品部分(三)
|
存储 索引
谷粒商城Es商品上架(1)
谷粒商城Es商品上架(1)
|
开发者
促销 | 学习笔记
快速学习促销。
86 0
|
开发者
促销组合策略 | 学习笔记
快速学习促销组合策略。
199 0
促销组合策略 | 学习笔记
|
设计模式 算法 开发工具
入职淘宝一年,聊聊我理解的首页&购物车
本篇文章的作者最近参与了淘宝购物车的工作,认为淘宝购物车和首页在体感上有很大区别,没有高屋建瓴地对整个交易体系和首页体系进行分析,只从一位一线开发的角度,写了写自己对购物车、对首页进行开发的感受和思考,欢迎指教讨论。
385 0
入职淘宝一年,聊聊我理解的首页&购物车
|
数据库 对象存储 黑灰产治理
阿里云购物车在哪?购物车满减代金券如何参与?
阿里云购物车满减代金券可领取三张,总面值1950元,分别为250元代金券、500元代金券和1200元代金券,阿小云来详细说下购物车使用方法及满减代金券说明
414 0
阿里云购物车在哪?购物车满减代金券如何参与?
2019年阿里云910会员节活动『优惠攻略篇』
2019阿里云910会员大促活动已经于8月28日正式开启,从已开放的活动页面来看,整场大促活动由阿里云10年有礼时光机、爆款产品推荐、七大分会场组成。 在910这个秋季大幅度优惠促销日,怎样才能花最少的钱配置最特惠的云服务?小编特为各位用户奉献出省钱大法如下: 丨活动阵地:https://www.