forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-ways-to-earn-points.cpp
44 lines (40 loc) · 1.14 KB
/
number-of-ways-to-earn-points.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Time: O(n * t * c)
// Space: O(t)
// knapsack dp
class Solution {
public:
int waysToReachTarget(int target, vector<vector<int>>& types) {
static const int MOD = 1e9 + 7;
vector<int> dp(target + 1);
dp[0] = 1;
for (const auto& t : types) {
for (int i = target; i >= 1; --i) {
for (int j = 1; j <= min(i / t[1], t[0]); ++j) {
dp[i] = (dp[i] + dp[i - j * t[1]]) % MOD;
}
}
}
return dp.back();
}
};
// Time: O(n * t * c)
// Space: O(t)
// knapsack dp
class Solution2 {
public:
int waysToReachTarget(int target, vector<vector<int>>& types) {
static const int MOD = 1e9 + 7;
vector<int> dp(target + 1);
dp[0] = 1;
for (const auto& t : types) {
vector<int> new_dp(target + 1);
for (int i = 0; i <= target; ++i) {
for (int j = 0; j <= min((target - i) / t[1], t[0]); ++j) {
new_dp[i + j * t[1]] = (new_dp[i + j * t[1]] + dp[i]) % MOD;
}
}
dp = move(new_dp);
}
return dp.back();
}
};