funcprofitableSchemes(n, minProfit int, group, profit []int) (sum int) { const mod int = 1e9 + 7 ng := len(group) dp := make([][][]int, ng+1) for i := range dp { dp[i] = make([][]int, n+1) for j := range dp[i] { dp[i][j] = make([]int, minProfit+1) } } dp[0][0][0] = 1 for i, members := range group { earn := profit[i] for j := 0; j <= n; j++ { for k := 0; k <= minProfit; k++ { if j < members { dp[i+1][j][k] = dp[i][j][k] } else { dp[i+1][j][k] = (dp[i][j][k] + dp[i][j-members][max(0, k-earn)]) % mod } } } } for _, d := range dp[ng] { sum = (sum + d[minProfit]) % mod } return }
funcmax(a, b int)int { if a > b { return a } return b }
classSolution { publicintprofitableSchemes(int n, int minProfit, int[] group, int[] profit) { int[][] dp = newint[n + 1][minProfit + 1]; for (inti=0; i <= n; i++) { dp[i][0] = 1; } intlen= group.length, MOD = (int)1e9 + 7; for (inti=1; i <= len; i++) { intmembers= group[i - 1], earn = profit[i - 1]; for (intj= n; j >= members; j--) { for (intk= minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - members][Math.max(0, k - earn)]) % MOD; } } } return dp[n][minProfit]; } }
[sol12-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicintProfitableSchemes(int n, int minProfit, int[] group, int[] profit) { int[,] dp = newint[n + 1, minProfit + 1]; for (int i = 0; i <= n; i++) { dp[i, 0] = 1; } int len = group.Length, MOD = (int)1e9 + 7; for (int i = 1; i <= len; i++) { int members = group[i - 1], earn = profit[i - 1]; for (int j = n; j >= members; j--) { for (int k = minProfit; k >= 0; k--) { dp[j, k] = (dp[j, k] + dp[j - members, Math.Max(0, k - earn)]) % MOD; } } } return dp[n, minProfit]; } }
[sol12-Python3]
1 2 3 4 5 6 7 8 9 10 11
classSolution: defprofitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int: MOD = 10**9 + 7 dp = [[0] * (minProfit + 1) for _ inrange(n + 1)] for i inrange(0, n + 1): dp[i][0] = 1 for earn, members inzip(profit, group): for j inrange(n, members - 1, -1): for k inrange(minProfit, -1, -1): dp[j][k] = (dp[j][k] + dp[j - members][max(0, k - earn)]) % MOD; return dp[n][minProfit]
[sol12-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var profitableSchemes = function(n, minProfit, group, profit) { const dp = newArray(n + 1).fill(0).map(() =>newArray(minProfit + 1).fill(0)); for (let i = 0; i <= n; i++) { dp[i][0] = 1; } const len = group.length, MOD = 1e9 + 7; for (let i = 1; i <= len; i++) { const members = group[i - 1], earn = profit[i - 1]; for (let j = n; j >= members; j--) { for (let k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - members][Math.max(0, k - earn)]) % MOD; } } } return dp[n][minProfit]; };
[sol12-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intprofitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit){ vector<vector<int>> dp(n + 1, vector<int>(minProfit + 1)); for (int i = 0; i <= n; i++) { dp[i][0] = 1; } int len = group.size(), MOD = (int)1e9 + 7; for (int i = 1; i <= len; i++) { int members = group[i - 1], earn = profit[i - 1]; for (int j = n; j >= members; j--) { for (int k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - members][max(0, k - earn)]) % MOD; } } } return dp[n][minProfit]; } };
[sol12-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intprofitableSchemes(int n, int minProfit, int* group, int groupSize, int* profit, int profitSize) { int dp[n + 1][minProfit + 1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i <= n; i++) { dp[i][0] = 1; } int len = groupSize, MOD = (int)1e9 + 7; for (int i = 1; i <= len; i++) { int members = group[i - 1], earn = profit[i - 1]; for (int j = n; j >= members; j--) { for (int k = minProfit; k >= 0; k--) { dp[j][k] = (dp[j][k] + dp[j - members][(int)fmax(0, k - earn)]) % MOD; } } } return dp[n][minProfit]; }
funcprofitableSchemes(n, minProfit int, group, profit []int) (sum int) { const mod int = 1e9 + 7 dp := make([][]int, n+1) for i := range dp { dp[i] = make([]int, minProfit+1) dp[i][0] = 1 } for i, members := range group { earn := profit[i] for j := n; j >= members; j-- { for k := minProfit; k >= 0; k-- { dp[j][k] = (dp[j][k] + dp[j-members][max(0, k-earn)]) % mod } } } return dp[n][minProfit] }
funcmax(a, b int)int { if a > b { return a } return b }
复杂度分析
时间复杂度:O(\textit{len} \times n \times \textit{minProfit}),其中 len 为数组 group 的长度。 动态规划需要计算的状态总数是 O(\textit{len} \times n \times \textit{minProfit}),每个状态的值需要 O(1) 的时间计算。