[LeetCode] Profitable Schemes

879. Profitable Schemes

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
class Solution {
int dp[101][101][101];
int mod = 1e9 + 7;
int helper(int index, int n, int mp, vector<int>& g, vector<int>& p) {
if(index == g.size()) return !mp;
if(n < 0) return 0;
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:
int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit) {
memset(dp,-1,sizeof(dp));
return helper(0,n,minProfit,group,profit);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/23/PS/LeetCode/profitable-schemes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.