Skip to content

Commit 48e9f44

Browse files
authored
Create profitable-schemes.cpp
1 parent a70126b commit 48e9f44

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/profitable-schemes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n * p * g)
2+
// Space: O(p * g)
3+
4+
class Solution {
5+
public:
6+
int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
7+
static const int M = 1000000007;
8+
vector<vector<int>> dp(P + 1, vector<int>(G + 1));
9+
dp[0][0] = 1;
10+
int result = 0;
11+
for (int k = 0; k < group.size(); ++k) {
12+
int g = group[k], p = profit[k];
13+
for (int i = P; i >= 0; --i)
14+
for (int j = G - g; j >= 0; --j)
15+
dp[min(i + p, P)][j + g] = (dp[min(i + p, P)][j + g] + dp[i][j]) % M;
16+
}
17+
for (const auto& p : dp[P]) {
18+
result = (result + p) % M;
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)