LeetCode 题解工作台
获得分数的方法数
考试中有 n 种类型的题目。给你一个整数 target 和一个下标从 0 开始的二维整数数组 types ,其中 types[i] = [count i , marks i ] 表示第 i 种类型的题目有 count i 道,每道题目对应 marks i 分。 返回你在考试中恰好得到 target …
2
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们定义 表示前 种类型的题目中,恰好得到 分的方法数。初始时 $f[0][0] = 1$,其余 $f[i][j] = 0$。答案即为 。 我们可以枚举第 种类型的题目,假设该类型题目的数量为 ,分数为 ,那么我们可以得到如下状态转移方程:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
考试中有 n 种类型的题目。给你一个整数 target 和一个下标从 0 开始的二维整数数组 types ,其中 types[i] = [counti, marksi] 表示第 i 种类型的题目有 counti 道,每道题目对应 marksi 分。
返回你在考试中恰好得到 target 分的方法数。由于答案可能很大,结果需要对 109 +7 取余。
注意,同类型题目无法区分。
- 比如说,如果有
3道同类型题目,那么解答第1和第2道题目与解答第1和第3道题目或者第2和第3道题目是相同的。
示例 1:
输入:target = 6, types = [[6,1],[3,2],[2,3]] 输出:7 解释:要获得 6 分,你可以选择以下七种方法之一: - 解决 6 道第 0 种类型的题目:1 + 1 + 1 + 1 + 1 + 1 = 6 - 解决 4 道第 0 种类型的题目和 1 道第 1 种类型的题目:1 + 1 + 1 + 1 + 2 = 6 - 解决 2 道第 0 种类型的题目和 2 道第 1 种类型的题目:1 + 1 + 2 + 2 = 6 - 解决 3 道第 0 种类型的题目和 1 道第 2 种类型的题目:1 + 1 + 1 + 3 = 6 - 解决 1 道第 0 种类型的题目、1 道第 1 种类型的题目和 1 道第 2 种类型的题目:1 + 2 + 3 = 6 - 解决 3 道第 1 种类型的题目:2 + 2 + 2 = 6 - 解决 2 道第 2 种类型的题目:3 + 3 = 6
示例 2:
输入:target = 5, types = [[50,1],[50,2],[50,5]] 输出:4 解释:要获得 5 分,你可以选择以下四种方法之一: - 解决 5 道第 0 种类型的题目:1 + 1 + 1 + 1 + 1 = 5 - 解决 3 道第 0 种类型的题目和 1 道第 1 种类型的题目:1 + 1 + 1 + 2 = 5 - 解决 1 道第 0 种类型的题目和 2 道第 1 种类型的题目:1 + 2 + 2 = 5 - 解决 1 道第 2 种类型的题目:5
示例 3:
输入:target = 18, types = [[6,1],[3,2],[2,3]] 输出:1 解释:只有回答所有题目才能获得 18 分。
提示:
1 <= target <= 1000n == types.length1 <= n <= 50types[i].length == 21 <= counti, marksi <= 50
解题思路
方法一:动态规划
我们定义 表示前 种类型的题目中,恰好得到 分的方法数。初始时 ,其余 。答案即为 。
我们可以枚举第 种类型的题目,假设该类型题目的数量为 ,分数为 ,那么我们可以得到如下状态转移方程:
其中 表示第 种类型的题目的数量。
最终的答案即为 。注意答案可能很大,需要对 取模。
时间复杂度 ,空间复杂度 。其中 为题目类型的数量;而 和 分别为题目的目标分数和每种类型题目的数量。
class Solution:
def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:
n = len(types)
mod = 10**9 + 7
f = [[0] * (target + 1) for _ in range(n + 1)]
f[0][0] = 1
for i in range(1, n + 1):
count, marks = types[i - 1]
for j in range(target + 1):
for k in range(count + 1):
if j >= k * marks:
f[i][j] = (f[i][j] + f[i - 1][j - k * marks]) % mod
return f[n][target]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Strong candidates will recognize that this is a state transition dynamic programming problem.
- question_mark
Candidates should efficiently handle large numbers and modulo operations.
- question_mark
An ideal solution will be able to justify their approach and optimize space usage.
常见陷阱
外企场景- error
Incorrect handling of the modulo operation, leading to overflow.
- error
Failure to correctly iterate over all possible question combinations.
- error
Not considering the constraints of the problem, such as the indistinguisability of questions of the same type.
进阶变体
外企场景- arrow_right_alt
Different limits for target and types array size.
- arrow_right_alt
Variation in question types where mark values are significantly larger.
- arrow_right_alt
Adding constraints on the count of questions for each type, such as having more than 50.