[LeetCode] Number of Ways to Earn Points

2585. Number of Ways to Earn Points

There is a test that has n types of questions. You are given an integer target and a 0-indexed 2D integer array types where types[i] = [counti, marksi] indicates that there are counti questions of the ith type, and each one of them is worth marksi points.

Return the number of ways you can earn exactly target points in the exam. Since the answer may be too large, return it modulo 109 + 7.

Note that questions of the same type are indistinguishable.

  • For example, if there are 3 questions of the same type, then solving the 1st and 2nd questions is the same as solving the 1st and 3rd questions, or the 2nd and 3rd questions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int waysToReachTarget(int target, vector<vector<int>>& types) {
vector<long long> dp(target + 1);
dp[0] = 1;
long long mod = 1e9 + 7;
for(auto t : types) {
long long c = t[0], m = t[1];
vector<long long> dpp = dp;
for(int i = 1; i <= c; i++) {
for(int j = target - i * m; j >= 0; j--) {
dpp[j + i * m] = (dpp[j + i * m] + dp[j]) % mod;
}
}
swap(dpp, dp);
}
return dp[target];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/05/PS/LeetCode/number-of-ways-to-earn-points/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.