考试中有n种类型的题目,给定整数target和二维数组types,其中types[i]=[count[i],marks[i]],表示第i种类型的题目有count[i]道,每道分值为marks[i]。求在考试中恰好得到target分的方法数,答案对1E9+7取模。注意,同类型题目无法区分。
1<=target<=1000; 1<=n<=50; 1<=count[i],marks[i]<=50
分析:同类型的题目,可以选0道、1道、2道...,枚举即可。
mint dp[55][1005];
class Solution {
public:
int waysToReachTarget(int target, vector<vector<int>>& types) {
int n = types.size();
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= target; j++) {
int cnt = types[i-1][0];
int mark = types[i-1][1];
dp[i][j] = dp[i-1][j];
for (int k = 1; k <= cnt && j >= k * mark; k++) {
dp[i][j] += dp[i-1][j-k*mark];
}
}
}
return dp[n][target].val();
}
};
标签:分数,题目,target,int,mark,leetcode2585,dp,方法,types
From: https://www.cnblogs.com/chenfy27/p/18666984