LeetCode每日一题(638. Shopping Offers)

In LeetCode Store, there are n 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 an integer array price where price[i] is the price of the ith item, and an integer array needs where needs[i] is the number of pieces of the ith item you want to buy.

You are also given an array special where special[i] is of size n + 1 where special[i][j] is the number of pieces of the jth item in the ith offer and special[i][n] (i.e., the last integer in the array) is the price of the ith offer.

Return the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.

Example 1:

Input: price = [2,5], special = [[3,0,5],[1,2,10]], needs = [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: price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [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.

Constraints:

  • n == price.length == needs.length
  • 1 <= n <= 6
  • 0 <= price[i], needs[i] <= 10
  • 1 <= special.length <= 100
  • special[i].length == n + 1
  • 0 <= special[i][j] <= 50

不知道这算不算是 greedy 算法, 我们求出当前步骤的最优解, 那当前步骤的上一步就可以根据这些最优解来计算上一步的最优解。最终得出整个问题的最优解。

拿到 needs 后我们先按正常的价格计算这些 needs 的总价,然后挨个尝试 special 中的组合,如果有可以用的组合则计算出使用一次该种组合的到的总价并与前面算出的总价做对比取最小值。最终得到的就是能满足当前 needs 的最低总价



use std::collections::HashMap;

impl Solution {
    fn dp(
        price: &Vec<i32>,
        special: &Vec<Vec<i32>>,
        needs: Vec<i32>,
        cache: &mut HashMap<Vec<i32>, i32>,
    ) -> i32 {
        if needs.iter().all(|v| v == &0) {
            return 0;
        }
        let mut ans: i32 = needs.iter().enumerate().map(|(i, v)| *v * price[i]).sum();
        for s in special {
            if needs.iter().enumerate().all(|(i, v)| v >= &s[i]) {
                let next_needs: Vec<i32> =
                    needs.iter().enumerate().map(|(i, v)| *v - s[i]).collect();
                let next_price = if let Some(c) = cache.get(&(next_needs)) {
                    *c
                } else {
                    Solution::dp(price, special, next_needs, cache)
                };
                ans = ans.min(next_price + s[s.len() - 1]);
            }
        }
        cache.insert(needs, ans);
        ans
    }
    pub fn shopping_offers(price: Vec<i32>, special: Vec<Vec<i32>>, needs: Vec<i32>) -> i32 {
        Solution::dp(&price, &special, needs, &mut HashMap::new())
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值