forked from WadekarPrashant/LeetCode-Problems-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem879.cpp
31 lines (28 loc) · 977 Bytes
/
Problem879.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
class Solution {
public:
int profitableSchemes(int n, int minProfit, vector<int>& group,
vector<int>& profit) {
constexpr int kMod = 1'000'000'007;
// dp[k][i][j] := # of schemes w/ first k crimes, AT MOST i members, and at
// Least j profits
vector<vector<vector<int>>> dp(
group.size() + 1,
vector<vector<int>>(n + 1, vector<int>(minProfit + 1)));
// No crimes, no profits, and any # of members
for (int i = 0; i <= n; ++i)
dp[0][i][0] = 1;
for (int k = 1; k <= group.size(); ++k) {
const int g = group[k - 1];
const int p = profit[k - 1];
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= minProfit; ++j)
if (i < g) {
dp[k][i][j] = dp[k - 1][i][j];
} else {
dp[k][i][j] = dp[k - 1][i][j] + dp[k - 1][i - g][max(0, j - p)];
dp[k][i][j] %= kMod;
}
}
return dp[group.size()][n][minProfit];
}
};