LeetCode 题解工作台

大礼包

在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有对应的价格。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。 给你一个整数数组 price 表示物品价格,其中 price[i] 是第 i 件物品的价格。另有一个整数数组 needs 表示购物清单,其中 needs[i]…

category

6

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们注意到,题目中物品的种类 $n \leq 6$,而每种物品需要购买的数量不超过 ,我们可以用 个二进制位来表示每种物品需要购买的数量,这样,我们只需要最多 $6 \times 4 = 24$ 个二进制位来表示整个购物清单。 我们首先将购物清单 转换为一个整数 ,其中第 个物品需要购买的数量存储在 的第 $i \times 4$ 位到第 $(i + 1) \times 4 - 1$ 位。…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有对应的价格。然而,也有一些大礼包,每个大礼包以优惠的价格捆绑销售一组物品。

给你一个整数数组 price 表示物品价格,其中 price[i] 是第 i 件物品的价格。另有一个整数数组 needs 表示购物清单,其中 needs[i] 是需要购买第 i 件物品的数量。

还有一个数组 special 表示大礼包,special[i] 的长度为 n + 1 ,其中 special[i][j] 表示第 i 个大礼包中内含第 j 件物品的数量,且 special[i][n] (也就是数组中的最后一个整数)为第 i 个大礼包的价格。

返回 确切 满足购物清单所需花费的最低价格,你可以充分利用大礼包的优惠活动。你不能购买超出购物清单指定数量的物品,即使那样会降低整体价格。任意大礼包可无限次购买。

 

示例 1:

输入:price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]
输出:14
解释:有 A 和 B 两种物品,价格分别为 ¥2 和 ¥5 。 
大礼包 1 ,你可以以 ¥5 的价格购买 3A 和 0B 。 
大礼包 2 ,你可以以 ¥10 的价格购买 1A 和 2B 。 
需要购买 3 个 A 和 2 个 B , 所以付 ¥10 购买 1A 和 2B(大礼包 2),以及 ¥4 购买 2A 。

示例 2:

输入:price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1]
输出:11
解释:A ,B ,C 的价格分别为 ¥2 ,¥3 ,¥4 。
可以用 ¥4 购买 1A 和 1B ,也可以用 ¥9 购买 2A ,2B 和 1C 。 
需要买 1A ,2B 和 1C ,所以付 ¥4 买 1A 和 1B(大礼包 1),以及 ¥3 购买 1B , ¥4 购买 1C 。 
不可以购买超出待购清单的物品,尽管购买大礼包 2 更加便宜。

 

提示:

  • 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
  • 生成的输入对于 0 <= j <= n - 1 至少有一个 special[i][j] 非零。
lightbulb

解题思路

方法一:状态压缩 + 记忆化搜索

我们注意到,题目中物品的种类 n6n \leq 6,而每种物品需要购买的数量不超过 1010,我们可以用 44 个二进制位来表示每种物品需要购买的数量,这样,我们只需要最多 6×4=246 \times 4 = 24 个二进制位来表示整个购物清单。

我们首先将购物清单 needs\textit{needs} 转换为一个整数 mask\textit{mask},其中第 ii 个物品需要购买的数量存储在 mask\textit{mask} 的第 i×4i \times 4 位到第 (i+1)×41(i + 1) \times 4 - 1 位。例如,当 needs=[1,2,1]\textit{needs} = [1, 2, 1] 时,有 mask=0b000100100001\textit{mask} = 0b0001 0010 0001

然后,我们设计一个函数 dfs(cur)\textit{dfs}(cur),表示当前购物清单的状态为 cur\textit{cur} 时,我们需要花费的最少金额。那么答案即为 dfs(mask)\textit{dfs}(\textit{mask})

函数 dfs(cur)\textit{dfs}(cur) 的计算方法如下:

  • 我们首先计算当前购物清单 cur\textit{cur} 不使用大礼包时的花费,记为 ans\textit{ans}
  • 然后,我们遍历每一个大礼包 offer\textit{offer},如果当前购物清单 cur\textit{cur} 能够使用大礼包 offer\textit{offer},即 cur\textit{cur} 中每种物品的数量都不小于大礼包 offer\textit{offer} 中的数量,那么我们可以尝试使用这个大礼包。我们将 cur\textit{cur} 中每种物品的数量减去大礼包 offer\textit{offer} 中的数量,得到一个新的购物清单 nxt\textit{nxt},然后递归计算 nxt\textit{nxt} 的最少花费,并加上大礼包的价格 offer[n]\textit{offer}[n],更新 ans\textit{ans},即 ans=min(ans,offer[n]+dfs(nxt))\textit{ans} = \min(\textit{ans}, \textit{offer}[n] + \textit{dfs}(\textit{nxt}))
  • 最后,返回 ans\textit{ans}

为了避免重复计算,我们使用一个哈希表 f\textit{f} 记录每一个状态 cur\textit{cur} 对应的最少花费。

时间复杂度 O(n×k×mn)O(n \times k \times m^n),其中 nn 表示物品的种类,而 kkmm 分别表示大礼包的数量以及每种物品的最大需求量。空间复杂度 O(n×mn)O(n \times m^n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
    def shoppingOffers(
        self, price: List[int], special: List[List[int]], needs: List[int]
    ) -> int:
        @cache
        def dfs(cur: int) -> int:
            ans = sum(p * (cur >> (i * bits) & 0xF) for i, p in enumerate(price))
            for offer in special:
                nxt = cur
                for j in range(len(needs)):
                    if (cur >> (j * bits) & 0xF) < offer[j]:
                        break
                    nxt -= offer[j] << (j * bits)
                else:
                    ans = min(ans, offer[-1] + dfs(nxt))
            return ans

        bits, mask = 4, 0
        for i, need in enumerate(needs):
            mask |= need << i * bits
        return dfs(mask)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate can efficiently manage state transitions and handle large numbers of special offers.

  • question_mark

    Observe how well the candidate applies memoization to avoid redundant calculations.

  • question_mark

    Assess whether the candidate effectively handles the backtracking approach to explore possible combinations.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for all possible states during the transition, which may lead to missed optimal solutions.

  • error

    Inefficient implementation of memoization, leading to excessive recomputation and slow performance.

  • error

    Failing to consider all combinations of offers, which can lead to suboptimal solutions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    How does the approach scale when the number of items increases beyond 6?

  • arrow_right_alt

    What if there were constraints limiting the number of times a single offer can be used?

  • arrow_right_alt

    Can this problem be solved using a greedy approach instead of dynamic programming?

help

常见问题

外企场景

大礼包题解:状态·转移·动态规划 | LeetCode #638 中等