There is a group of n members, and a list of various crimes they could commit. The ith crime generates a profit[i] and requires group[i] members to participate in it. If a member participates in one crime, that member can’t participate in another crime.
Let’s call a profitable scheme any subset of these crimes that generates at least minProfit profit, and the total number of members participating in that subset of crimes is at most n.
Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo 109 + 7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { int dp[101][101][101]; int mod = 1e9 + 7; inthelper(int index, int n, int mp, vector<int>& g, vector<int>& p){ if(index == g.size()) return !mp; if(n < 0) return0; int& res = dp[index][n][mp]; if(res != -1) return res; res = helper(index + 1, n, mp, g, p); if(g[index] <= n) res = (res + helper(index + 1, n - g[index], max(0,mp - p[index]), g, p)) % mod; return res; } public: intprofitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit){ memset(dp,-1,sizeof(dp)); returnhelper(0,n,minProfit,group,profit); } };