Skip to content

Commit 4dedebe

Browse files
authored
Create number-of-ways-to-earn-points.cpp
1 parent 852cbe7 commit 4dedebe

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

C++/number-of-ways-to-earn-points.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Time: O(n * t * c)
2+
// Space: O(t)
3+
4+
// knapsack dp
5+
class Solution {
6+
public:
7+
int waysToReachTarget(int target, vector<vector<int>>& types) {
8+
static const int MOD = 1e9 + 7;
9+
10+
vector<int> dp(target + 1);
11+
dp[0] = 1;
12+
for (const auto& t : types) {
13+
for (int i = target; i >= 1; --i) {
14+
for (int j = 1; j <= min(i / t[1], t[0]); ++j) {
15+
dp[i] = (dp[i] + dp[i - j * t[1]]) % MOD;
16+
}
17+
}
18+
}
19+
return dp.back();
20+
}
21+
};
22+
23+
// Time: O(n * t * c)
24+
// Space: O(t)
25+
// knapsack dp
26+
class Solution2 {
27+
public:
28+
int waysToReachTarget(int target, vector<vector<int>>& types) {
29+
static const int MOD = 1e9 + 7;
30+
31+
vector<int> dp(target + 1);
32+
dp[0] = 1;
33+
for (const auto& t : types) {
34+
vector<int> new_dp(target + 1);
35+
for (int i = 0; i <= target; ++i) {
36+
for (int j = 0; j <= min((target - i) / t[1], t[0]); ++j) {
37+
new_dp[i + j * t[1]] = (new_dp[i + j * t[1]] + dp[i]) % MOD;
38+
}
39+
}
40+
dp = move(new_dp);
41+
}
42+
return dp.back();
43+
}
44+
};

0 commit comments

Comments
 (0)